Android:具有复杂数据模型的ListView
我想将“复杂”数据数组映射到 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),这确实很有帮助。
I'd like to map an Array of "complex" data to a ListView. In a very simplified form my data model would look like something like this:
class ListPlacesValues {
String idObject;
String name;
String city;
String country;
ArrayList<String> classification;
double distance_quantity;
DistanceUnit distance_unit;
[...more stuff ...]
}
I know that I can convert my complex data into a HashList and then just use a 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}
);
However, I would rather use my data model directly, but I've no idea where and how to start, so that in the end I can do something like this:
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}
);
Solution: I found this Android API sample (List14), which was really helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以扩展ArrayAdapter。这是给您的代码示例。在此示例中 - SearchItem 是一些自定义 POJO。基本上,您需要重写 getView() 方法来通过膨胀行布局来构建行,然后根据项目列表和当前位置填充值
You can extend ArrayAdapter. Here's code example for you. In this example - SearchItem is some custom POJO. Basically you need to override getView() method to build your row by inflating row layout and then populating values based on List of items and current position
在您链接的示例中,convertView 有一个陷阱,
您希望将所有动画或未使用的字段设置为 null,否则您的项目中可能包含您不想要的数据或待处理的动画。
There is one pitfall with the convertView in the sample you linked
you want to set all animations or unused fields to null otherwise your items might have data in them or animations pending you dont want.