【问题标题】:Android: ListView with complex data modelAndroid:具有复杂数据模型的 ListView
【发布时间】:2009-10-20 15:22:05
【问题描述】:

我想将一组“复杂”数据映射到 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),它真的很有帮助。

【问题讨论】:

标签: android listview adapter


【解决方案1】:

您可以扩展 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;
}
}

【讨论】:

  • 谢谢。这有很大帮助。我刚刚发现的这个 API 示例也很有帮助:developer.android.com/guide/samples/ApiDemos/src/com/example/…
  • 当然你不想那样实现getView(),这只是一个例子(使用convertView)。
  • 您不需要维护自己的items 列表,当您扩展ArrayAdapter 时,它会为您提供存储空间。您应该做的就是致电add。如果您想维护自己的存储,请使用BaseAdapter
  • @znk 链接坏了。
【解决方案2】:

在您链接的示例中,convertView 存在一个缺陷

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

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

【讨论】:

    猜你喜欢
    • 2011-01-17
    • 2017-05-18
    • 1970-01-01
    • 2010-11-16
    • 1970-01-01
    • 2016-09-26
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多