我想将一组“复杂”数据映射到 ListView。在一个非常简化的形式中,我的数据模型看起来像这样:
class ListPlacesValues {
String idObject;
String name;
String city;
String country;
ArrayList<String> classification;
double distance_quantity;
DistanceUnit distance_unit;
[...more stuff ...]
}
我知道我可以将复杂数据转换为 HashList,然后只需使用 SimpleAdapter:
SimpleAdapter mAdapter = new SimpleAdapter(
this,
hashList,
R.layout.places_listitem,
new String[] { "name", "city", "country"},
new int[] { R.id.name, R.id.city, R.id.country}
);
但是,我宁愿直接使用我的数据模型,但我不知道从哪里以及如何开始,所以最后我可以做这样的事情:
ArrayList<ListPlacesValues> values = getData();
MyAdapter mAdapter = new MyAdapter(
this,
values,
R.layout.places_listitem,
ListPlacesValues { values.name, values.city, values.country},
new int[] { R.id.name, R.id.city, R.id.country}
);
解决方案:我找到了这个 Android API 示例(List14),它真的很有帮助。