0
我目前實施分區域列表視圖適配器PinnedSectioned Listview如何通過哈希映射數據BaseAdapter Android中
目前我已經改變了實施基礎適配器,但我在值傳遞給底座適配器面臨的一些問題
如何將Hashmap值傳遞給適配器。
例 -
HashMap<Integer, ArrayList<Test>> example = new HashMap<Integer, ArrayList<Test>>();
基地適配器實現
static class MyPinnedSectionListAdapter extends BaseAdapter implements PinnedSectionListView.PinnedSectionListAdapter{
private ArrayList mList;
private Context mContext;
private LayoutInflater mInflater;
ArrayList<Item> value;
public static final int FIRST = 0;
public static final int SECOND = 1;
int size=0;
private String[] mKeys;
private Map<String, ArrayList<Item>> mData = new HashMap<String, ArrayList<Item>>();
public MyPinnedSectionListAdapter(Context context, Map<String, ArrayList<Item>> map){
mList=new ArrayList();
mData=map;
mKeys = mData.keySet().toArray(new String[mData.size()]);
for (Map.Entry<String, ArrayList<Item>> entry : map.entrySet()) {
value= entry.getValue();
size+=value.size();
System.out.println();
}
System.out.println("size" + size);
mList.addAll(map.entrySet());
this.mContext = context;
this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public boolean isItemViewTypePinned(int viewType) {
if(viewType == SECOND){
return true;
}else{
return false;
}
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(mKeys[position]);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
int viewType = getItemViewType(position);
if(viewType == FIRST){
convertView = mInflater.inflate(R.layout.item,parent,false);
String key = mKeys[position];
String Value = getItem(position).toString();
((TextView)convertView.findViewById(R.id.textView1)).setText(Value);
}
if(viewType == SECOND){
String key = mKeys[position];
String Value = getItem(position).toString();
convertView = mInflater.inflate(R.layout.section,parent,false);
((TextView)convertView.findViewById(R.id.textView1)).setText(Value);
}
return convertView;
}
@Override
public int getItemViewType(int position) {
@SuppressWarnings("unchecked")
ArrayList<Item> item = (ArrayList<Item>) getItem(position);
if (item.get(position).type == 0){
return FIRST;
}else{
return SECOND;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
}
POJO:
static class Item {
public static final int ITEM = 0;
public static final int SECTION = 1;
public final int type;
public final String text;
public int sectionPosition;
public int listPosition;
public Item(int type, String text) {
this.type = type;
this.text = text;
}
@Override public String toString() {
return text;
}
}
CODE:
HashMap> example = new HashMap>();
ArrayList<Item> test1 = new ArrayList<Item>();
test1.add(new Item(Item.SECTION, "Section1"));
test1.add(new Item(Item.ITEM, "Section2"));
test1.add(new Item(Item.ITEM, "Section3"));
ArrayList<Item> test2 = new ArrayList<Item>();
test2.add(new Item(Item.SECTION, "Section4"));
test2.add(new Item(Item.ITEM, "Section5"));
test2.add(new Item(Item.ITEM, "Section6"));
example.put("1", test1);
example.put("2", test2);
setListAdapter(new MyPinnedSectionListAdapter(this,example));
我正在輸出中,如:
[SECTION1,[第2],[Section3中]
【第4],[第5節],[第6]
我試過了,但Integer代表節和Arraylist代表項目如何可以返回大小? – 2014-10-29 20:26:15
爲此,您必須製作自定義適配器。其大小相應地按照其''getCount()'分區Arraylist的大小。 – user370305 2014-10-29 20:27:17
好的。還有其他方法可以實現嗎?而不是傳遞Hashmap? – 2014-10-29 20:31:29