Android:具有复杂数据模型的ListView

发布于 2024-08-07 21:04:50 字数 1202 浏览 2 评论 0原文

我想将“复杂”数据数组映射到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生寂 2024-08-14 21:04:50

您可以扩展ArrayAdapter。这是给您的代码示例。在此示例中 - SearchItem 是一些自定义 POJO。基本上,您需要重写 getView() 方法来通过膨胀行布局来构建行,然后根据项目列表和当前位置填充值

class SearchItemsAdapter extends ArrayAdapter<SearchItem> {
Activity context;
List<SearchItem> items;
SearchHeader header;

@SuppressWarnings("unchecked")
public SearchItemsAdapter(final Activity context,
        final Map<SearchHeader, List<SearchItem>> result) {
    super(context, R.layout.item, (List) ((Object[]) result.values()
            .toArray())[0]);
    this.context = context;
    this.header = result.keySet().iterator().next();
    this.items = result.get(this.header);
}

@Override
public View getView(final int position, final View convertView,
        final ViewGroup parent) {
    final View view = this.context.getLayoutInflater().inflate(
            R.layout.item, null);
    final SearchItem item = this.items.get(position);
    ((TextView) view.findViewById(R.id.jt)).setText(item.jt);
    ((TextView) view.findViewById(R.id.dp)).setText(item.dp);
    ((TextView) view.findViewById(R.id.cn)).setText(item.cn);
    ((TextView) view.findViewById(R.id.loc)).setText(item.loc.name);
    final TextView body = ((TextView) view.findViewById(R.id.e));
    body.setText(item.e);
    body.setTag(item.src[0]);
    ((TextView) view.findViewById(R.id.src)).setText(item.src[1]);
    return view;
}
}

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

class SearchItemsAdapter extends ArrayAdapter<SearchItem> {
Activity context;
List<SearchItem> items;
SearchHeader header;

@SuppressWarnings("unchecked")
public SearchItemsAdapter(final Activity context,
        final Map<SearchHeader, List<SearchItem>> result) {
    super(context, R.layout.item, (List) ((Object[]) result.values()
            .toArray())[0]);
    this.context = context;
    this.header = result.keySet().iterator().next();
    this.items = result.get(this.header);
}

@Override
public View getView(final int position, final View convertView,
        final ViewGroup parent) {
    final View view = this.context.getLayoutInflater().inflate(
            R.layout.item, null);
    final SearchItem item = this.items.get(position);
    ((TextView) view.findViewById(R.id.jt)).setText(item.jt);
    ((TextView) view.findViewById(R.id.dp)).setText(item.dp);
    ((TextView) view.findViewById(R.id.cn)).setText(item.cn);
    ((TextView) view.findViewById(R.id.loc)).setText(item.loc.name);
    final TextView body = ((TextView) view.findViewById(R.id.e));
    body.setText(item.e);
    body.setTag(item.src[0]);
    ((TextView) view.findViewById(R.id.src)).setText(item.src[1]);
    return view;
}
}
东风软 2024-08-14 21:04:50

在您链接的示例中,convertView 有一个陷阱,

if(convertView != null){ //reuse
   convertView.setAnimation(null);
   convertView.setAnyCustomFieldsIdontWantFilledWithData(null);
}

您希望将所有动画或未使用的字段设置为 null,否则您的项目中可能包含您不想要的数据或待处理的动画。

There is one pitfall with the convertView in the sample you linked

if(convertView != null){ //reuse
   convertView.setAnimation(null);
   convertView.setAnyCustomFieldsIdontWantFilledWithData(null);
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文