安卓的ListView复杂的数据模型 [英] Android: ListView with complex data model

查看:139
本文介绍了安卓的ListView复杂的数据模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想的复杂的数据数组映射到一个ListView。在一个非常简化的形式我的数据模型看起来就像是这样的:

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 ...]
}

我知道,我可以把我的复杂的数据到一个HashList,然后只用一个SimpleAdapter:

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}
);  


解决方法:我发现这个Android的API示例(<一href="http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List14.html"相对=nofollow> List14 ),这是非常有帮助的。


Solution: I found this Android API sample (List14), which was really helpful.

推荐答案

您可以扩展ArrayAdapter。这里是您code例子。在这个例子中 - 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

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;
}
}

这篇关于安卓的ListView复杂的数据模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆