Having access to a view of a listView with SimpleAdapter

301 Views Asked by At

In my program, as soon as I create a ListView and I set a Simpleadapter as adapter, I try to get access of a View in this listView in order to change the background of this view depending on a condition. I do it with the method ListView.getChildAt(position). However, I get a nullPointer Exception and i do not understand why. Here is a part of my code that is concerned. To better understand the code below: I have actually created 2 listViews in the code and Alarm is a class I have implemented. I simply retrieve some pieces of information through this class.

Java Code:

public class SmartAlarm extends AppCompatActivity {
    private ListView list_view_alarms;
    private ListView list_view_activates;
    private List<HashMap<String, String>> listMapOfEachAlarm;
    private List<HashMap<String, Integer>> listMapOfActivates;
    private SimpleAdapter adapter_alarms;
    private SimpleAdapter adapter_activates;

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_smart_alarm);
        list_view_alarms = (ListView) findViewById(R.id.list_alarm);
        list_view_activates = (ListView) findViewById(R.id.list_activate);
        listMapOfEachAlarm = new ArrayList<>();
        listMapOfActivates = new ArrayList<>();
        adapter_alarms = new SimpleAdapter(this, listMapOfEachAlarm, R.layout.item_alarm,
new String[]{"alarm", "title"}, new int[]{R.id.time, R.id.title});
        adapter_activates = new SimpleAdapter(this, listMapOfActivates, R.layout.item_activate, new String[]{"alarm_drawable"}, new int[]{R.id.activate});
        for (Alarm alarm : alarmList) {
            HashMap<String, String> mapOfTheNewAlarm = new HashMap<>();
            mapOfTheNewAlarm.put("alarm", alarm.getTime());
            mapOfTheNewAlarm.put("title", alarm.getTitle());
            listMapOfEachAlarm.add(mapOfTheNewAlarm);
            HashMap<String, Integer> mapOfTheAlarmDrawable = new HashMap<>();
            if (alarm.getActivated()) {
                mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_on);
            } else {
                mapOfTheAlarmDrawable.put("alarm_drawable", R.drawable.alarm_off);
            }
            listMapOfActivates.add(mapOfTheAlarmDrawable);
        }
        list_view_alarms.setAdapter(adapter_alarms);
        list_view_activates.setAdapter(adapter_activates);
        for(int i=0; i<list_view_alarms.getCount();i++)
        {
            if(conditionRespected()){
                list_view_alarms.getChildAt(i)
                   .setBackgroundColor(getResources().getColor (R.color.dark)); //The compilation error is here because list_view_alarms.getChildAt(i) is null

            }
        }
    }
}

activity_smart_alarm.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/crazy_alarm"
android:orientation="vertical"
android:weightSum="10">

<CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:text="Check the box if you want to activate the game" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ListView
        android:id="@+id/list_alarm"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:choiceMode="singleChoice"
        android:divider="#FF0000"
        android:dividerHeight="2dp" />

    <ListView
        android:id="@+id/list_activate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:choiceMode="singleChoice"
        android:divider="#FF0000"
        android:dividerHeight="2dp" />

</LinearLayout>

</LinearLayout>
2

There are 2 best solutions below

0
On BEST ANSWER

For people interested: To change the background of each view in the listView according to a specific condition, I have finally overrided the method getView() like this :

public View getView(int position, View convertView, ViewGroup parent)
    {
        convertView = super.getView(position, convertView,  parent);
        if(condition){
            convertView.setBackgroundColor(getResources().getColor(R.color.dark));
        }
        else
        {
            convertView.setBackgroundColor(getResources().getColor(R.color.bright));
        }
        return convertView;
    }
8
On

You aren't setting your main view. Your listview must be created inside of the view that your activity will actually be using. Right inside of your onCreate() add setContentView(R.layout.activity_main);

You also have to create a layout (in this case activity_main) and your listView has to be child of the activity_main layout

This might be what your activity_main looks like

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

     //and inside of here would be your listview
     <android.support.v7.widget.ListViewCompat
        android:id="@+id/list_alarm"
        android:layout_width=""
        android:layout_height=""></android.support.v7.widget.ListViewCompat>

</RelativeLayout>