gpt4 book ai didi

java - 使用 SimpleAdapter 访问 listView 的 View

转载 作者:行者123 更新时间:2023-12-02 12:18:36 24 4
gpt4 key购买 nike

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

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>

最佳答案

对于感兴趣的人:为了根据特定条件更改listView中每个 View 的背景,我最终重写了方法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;
}

关于java - 使用 SimpleAdapter 访问 listView 的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45969788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com