简体   繁体   English

使用SimpleAdapter可以访问listView的视图

[英]Having access to a view of a listView with SimpleAdapter

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. 在我的程序中,一旦创建ListView并将Simpleadapter设置为适配器,我就会尝试访问此listView中的View,以便根据条件更改该视图的背景。 I do it with the method ListView.getChildAt(position). 我用方法ListView.getChildAt(position)来做。 However, I get a nullPointer Exception and i do not understand why. 但是,我得到一个nullPointer异常,我不明白为什么。 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. 为了更好地理解以下代码:我实际上在代码中创建了2个listViews,并且Alarm是我实现的类。 I simply retrieve some pieces of information through this class. 我只是通过此类检索一些信息。

Java Code: 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: 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>

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 : 对于有兴趣的人:要根据特定条件更改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;
    }

You aren't setting your main view. 您没有设置主视图。 Your listview must be created inside of the view that your activity will actually be using. 您的listview必须在您的活动将实际使用的视图内部创建。 Right inside of your onCreate() add setContentView(R.layout.activity_main); 在您的onCreate()内部添加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 您还必须创建一个布局(在本例中为activity_main ),并且listView必须是activity_main布局的子级

This might be what your activity_main looks like 这可能是您的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM