gpt4 book ai didi

Android 将项目添加到仅包含特定项目的 Listview.setAdapter

转载 作者:行者123 更新时间:2023-11-30 02:21:09 26 4
gpt4 key购买 nike

有没有可能使用仅包含特定项目的 setAdapter 在 ListView 中添加项目?例如,我想在 ListView 中添加一个仅包含日期“2015 年 2 月 15 日”的项目。

代码如下:

   final ListAdapter adapter = new SimpleAdapter(CalendarActivity.this, eventsList, R.layout.calendar_event_list, new String[]{TAG_PID,
TAG_EVENTTITLE,TAG_EVENTSTART,TAG_EVENTEND},
new int[]{R.id.pid, R.id.eventname, R.id.eventstart, R.id.eventend});
ListView myList = (ListView) findViewById(android.R.id.list);

SimpleDateFormat formatdate = new SimpleDateFormat("MMM. dd, yyyy");
String selecdate = formatdate.format(date);

if (eventDates.contains(selecdate)) {
myList.setAdapter(adapter); //these line I want only to add items in listview which contains the value of 'selectdate'
}
else {
myList.setAdapter(null);

}

最佳答案

SimpleAdapter 用于静态数据。由于您想要动态过滤数据,因此您应该实现自定义适配器来执行此操作。但是,一个廉价的解决方案是在设置适配器之前过滤数据。像这样:

ListView myList = (ListView) findViewById(android.R.id.list);
SimpleDateFormat formatdate = new SimpleDateFormat("MMM. dd, yyyy");
String selecdate = formatdate.format(date);
// Filter selected events
List<Map<String, Object>> filteredEventsList = new ArrayList<Map<String, Object>>();
for (Map<STring, Object> row : eventsList) {
if (row should be shown) {
filteredEventsList.add(row);
}
}

final ListAdapter adapter = new SimpleAdapter(CalendarActivity.this, filteredEventsList, R.layout.calendar_event_list, new String[]{TAG_PID,
TAG_EVENTTITLE,TAG_EVENTSTART,TAG_EVENTEND},
new int[]{R.id.pid, R.id.eventname, R.id.eventstart, R.id.eventend});
myList.setAdapter(adapter);

关于Android 将项目添加到仅包含特定项目的 Listview.setAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28525580/

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