当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Creating a custom hashmapadapter

问题描述:

If I have a hashmap containing the following:

Hashmap contains (String, String)

How can I instantiate a custom adapter? The custom adapter should extend baseadapter.

I need to combine both key and value so it looks something like "KEY+VALUE", "KEY+VALUE"... and assign this to an array VALUES. The array VALUES is used later on when I insantiate my custom adpter.

Instantiation should look something like this:

MyCustomAdapter adapter = new MyCustomAdapter(this, android.R.layout.simple_list_item_1, VALUES);

setListAdapter(adapter)

I am lost here so code would be a big help.

THANKS

graham

The following list is using as its datasource a string array called items.

public ArrayList<String> myList = new ArrayList<String>(Arrays.asList(items));

however items is a string array which I would like to stop using and instead start using concatenated key+value pairs from my hashmap

so instead of the user being presented a list of items he will be presented a list of key+value pairs taken from the hashmap hm

网友答案:

I don't think you need to use a custom adapter. Your layout is quite simple, you need only a textView, so you can use ArrayAdapter. For you example you can do:

HashMap<Integer,String>hm=new HashMap<Integer,String>();
Vector<String>elements=new Vector<String>();
 for(int i=0; i<=10;i){      
      hm.put(i,("num"+i));
    }
    for (Entry<Integer, String> e : hm.entrySet()) {
        String newString=e.toString();
        elements.add(newString);
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, elements);
    list.setAdapter(adapter);
分享给朋友:
您可能感兴趣的文章:
随机阅读: