2013. 6. 25. 15:48

There are no predefined Adapters which will render a HashMap. I suggest creating your own Adapter by extending BaseAdapter.

Edit: It is posible to use HashMap with and extended BaseAdapter, here's an(untested) example:

public class HashMapAdapter extends BaseAdapter {

    private HashMap<String, String> mData = new HashMap<String, String>();
    private String[] mKeys;
    public HashMapAdapter(HashMap<String, String> data){
        mData  = data;
        mKeys = mData.keySet().toArray(new String[data.size()]);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        return mData.get(mKeys[position]);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    @Override
    public View getView(int pos, View convertView, ViewGroup parent) {
        String key = mKeys[pos];
        String Value = getItem(pos).toString();

        //do your view stuff here

        return convertView;EDIT
    }
}

This comes with the following caveat, the order of the items is not guaranteed to be the same order you added them. Writing this example has made me realize; Don't use HashMap in an adapter :)

'Android > 개발팁' 카테고리의 다른 글

[Android] keyboard 감지  (0) 2013.07.08
How to replace R.drawable.“someString”  (0) 2013.06.25
What adapter shall I use to use HashMap in a ListView  (0) 2013.06.25
Intent 로 gallery 의 특정 폴더 띄우기.  (1) 2013.05.31
Android log 작성법  (0) 2013.05.22
패키지 네이밍 !  (0) 2013.05.07
Posted by hoonihoon

댓글을 달아 주세요