您可能想要使用已订购的地图,如TreeMap或LinkedHashMap,并将其传递到您的适配器。
这将启用你想要实现的。
你基本上需要有利用键值对数据结构(图)适配器,并设置它在你的列表视图,是这样的:
public class OrderedMapAdapter extends BaseAdapter {
Map.Entry[] entries;
/***
* Pass the key value pair map using an ordered map data structure
* @param orderedMap An ordered map, either LinkedHashMap or TreeMap
*/
public OrderedMapAdapter(LinkedHashMap orderedMap) {
this.entries = orderedMap.entrySet().toArray(new Map.Entry[0]);
}
public int getCount() {
return entries.length;
}
/***
* This should get you the position you want in Key-value pair format
* that you could use whenever the row is selected
*/
public Map.Entry getItem(int position) {
return entries[position];
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// display your view here
return null;
}
}
你可以请发布示例代码? –