开发者

How do I get the value from HashMap in my ListView?

I've created my own custom adapter by extending BaseAdapter. I just want to be able to grab the string of the value in my onItemClick method (so I can put it as an intent extra). How can I do that?

My custom adapter looks like this:

   private class HashMapAdapter extends BaseAdapter {

        private HashMap<String, String> mMap = new HashMap<String, String>();
        private String[] mKeys;
        private Context mContext;

        private HashMapAdapter(Context context, HashMap<String, String> map) {
            mContext = context;
            mMap = map;
            mKeys = mMap.keySet().toArray(new String[map.size()]);          
        }

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

        @Override
        public Object getItem(int position) {

            return mMap.get(mKeys[position]);
        }

        @Overr开发者_运维百科ide
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
//          String key = mKeys[position];

            RowView row;
            if(convertView == null) {
                row = new RowView(getBaseContext());
            } else {
                row = (RowView) convertView;
            }
            row.display(mKeys[position]);
            return row;

        }

    }


Not sure why I couldn't get it to work before, but this is how to do it:

intent.putExtra("url",  arg0.getAdapter().getItem(arg2).toString());


In the getView method you can retrieve the value by using

mMap.get(mKeys[position]);

although I'm not 1000% sure


Use LinkedHashMap and get value according to position using

ArrayList<LinkedHashMap<Integer, String>> myCalandersAvailable2

In getView

String value = (new ArrayList<String>(myCalandersAvailable2
                .get(position).values())).get(position);

It Will give you idea how to do.

or use this method to get value according to position from LinkedHashMap

public Object getElementAt(LinkedHashMap map, int index) {
    for (Map.Entry entry : map.entrySet()) {
        if (index-- == 0) {
            return entry.value();
        }
    }
    return null;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜