如何从 android 中的 Listadapter 获取项目?
How to get items from Listadapter in android?
我有一个 Listadapter,其中有 4 个不同的字符串,并将它们存储到我的列表视图中。现在我想从其中一个字符串中获取所有项目并将其解析为 "date",那么我怎样才能从我的列表适配器填充我的日历日期?
遵循代码:
// 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();
}
}
如果你想从你的适配器中获取一个项目,你可以使用
adapter.getItem(position);
这将 return 指定位置的项目。在您的情况下,该方法将 return 指定位置的 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());
}
问题解决了!只需简单地放入循环并将 'position' 变量更改为处理循环的变量,例如 for (int i = 0; i < eventsList.size(); i++)
将 adapter.getItem(position)
更改为 adapter.getItem(i)
我有一个 Listadapter,其中有 4 个不同的字符串,并将它们存储到我的列表视图中。现在我想从其中一个字符串中获取所有项目并将其解析为 "date",那么我怎样才能从我的列表适配器填充我的日历日期?
遵循代码:
// 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();
}
}
如果你想从你的适配器中获取一个项目,你可以使用
adapter.getItem(position);
这将 return 指定位置的项目。在您的情况下,该方法将 return 指定位置的 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());
}
问题解决了!只需简单地放入循环并将 'position' 变量更改为处理循环的变量,例如 for (int i = 0; i < eventsList.size(); i++)
将 adapter.getItem(position)
更改为 adapter.getItem(i)