是否有任何可能的方法使用仅包含特定项目的setAdapter在listview中添加项目?例如,我想在listview中添加一个包含日期为" 2月的项目。 15,2015"仅
遵循以下代码:
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);
}
答案 0 :(得分:1)
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);