安卓的ListView复杂的数据模型复杂、数据模型、ListView

由网友(你不缺我i)分享简介:我想的复杂的数据数组映射到一个ListView。在一个非常简化的形式我的数据模型看起来就像是这样的:I'd like to map an Array of "complex" data to a ListView. In a very simplified form my data model would look...

我想的复杂的数据数组映射到一个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示例( 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;
}
}
阅读全文

相关推荐

最新文章