我有一个Listadapter,其中有4个不同的字符串,并将它们存储到我的listview中。现在我想从其中一个字符串中获取所有项目并将其解析为" date",那么如何才能从listadapter中填充我的日历日期?
遵循以下代码:
// Get User records from SQLite DB
ArrayList<HashMap<String, String>> eventsList = controller.getAllevents();
// If users exists in SQLite DB
if (eventsList.size() != 0) {
// Set the User Array list in ListView
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);
myList.setAdapter(adapter);
String eventstart = adapter.toString(); //I got null exception here..
Date edate = ParseDate(eventstart);
if (caldroidFragment != null) {
caldroidFragment.setBackgroundResourceForDate(R.color.Green, edate);
caldroidFragment.refreshView();
}
}
答案 0 :(得分:1)
如果您想从适配器中获取项目,可以使用
adapter.getItem(position);
将返回指定位置的项目。在您的情况下,该方法将返回指定位置的HashMap<String, String>
:
示例:
/* adapter.getCount() returns the count of how many items
(HashMaps, in your case) that is represented in this adapter. */
for(int i = 0; i < adapter.getCount(); i++){
HashMap<String, String> myHashMap = adapter.getItem(i);
//"myKey" is the key that you provided, when mapping your key-values
String myVal = myHashMap.get("myKey");
Date edate = ParseDate(myVal);
/* Handle your Date object here. I'm just printing it to the console
in this example. */
System.out.println("Item at position " + i + " " + edate.toString());
}
答案 1 :(得分:0)
问题解决了!只需简单地放入循环并将'position'变量更改为处理循环的变量,例如for (int i = 0; i < eventsList.size(); i++)
将adapter.getItem(position)
更改为adapter.getItem(i)