使用 SimpleAdapter 访问 listView 的视图

Having access to a view of a listView with SimpleAdapter

在我的程序中,一旦我创建了一个 ListView 并将一个 Simpleadapter 设置为适配器,我就尝试访问此 listView 中的一个视图,以便根据条件更改此视图的背景。我使用方法 ListView.getChildAt(position) 来完成。但是,我得到一个 nullPointer 异常,我不明白为什么。这是我的代码的一部分。 为了更好地理解下面的代码:我实际上在代码中创建了 2 个 listView,而 Alarm 是我实现的 class。我只是通过这个class.

检索了一些信息

Java代码:

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>

您没有设置主视图。您的列表视图必须在您的 activity 实际使用的视图内创建。在 onCreate() 的右侧添加 setContentView(R.layout.activity_main);

您还必须创建布局(在本例中为 activity_main)并且您的 listView 必须是 activity_main 布局的子布局

这可能就是您的 activity_main 的样子

<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>

对于感兴趣的人:要根据特定条件更改 listView 中每个视图的背景,我终于像这样重写了方法 getView() :

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;
    }