Creating a custom hashmapadapter

Go To StackoverFlow.com

0

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

2012-04-04 07:35
by user803271
I really don't understand what you are trying to do... If you want to show to the user a list of key+value pairs you have just to use the code in my answer. Try to see this link - Ant4res 2012-04-04 12:40


1

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);
2012-04-04 08:51
by Ant4res
Hi, It looks better now but in my list I get 0=num1.... I want populate it with the values for the following hashmap hm.put ("one", "Apple"); hm.put("two", "Mango"); hm.put("three", "Grape"); the values and keys should be concented in elements thank - user803271 2012-04-04 10:08
FIGURED IT OUT for(int i=0; i<=10;i++) { hm.put(1, "Apple"); hm.put(2, "Mango"); hm.put(3, "Grape"); hm.put(4, "Orange"); hm.put(5, "Peach"); } for (Entry e : hm.entrySet()) { String newString=e.toString(); elements.add(newString); - user803271 2012-04-04 10:10
How do you want them to appear on the list? Only Apple, Mango and Grape? To do that you have only to change the e.toString() with e.getValue().toString() - Ant4res 2012-04-04 10:11
Hi, I see one more problem i have declared public ArrayList myList = new ArrayList(Arrays.asList(items)); and items is a string array. How can I update this to incluce hm.put(1, "Apple");
hm.put(2, "Mango");
hm.put(3, "Grape");
hm.put(4, "Orange");
hm.put(5, "Peach") - user803271 2012-04-04 10:15
What is myList and what items contain? Could you please edit your first question with your code, otherwise I can't understand what you are trying to do - Ant4res 2012-04-04 10:24
updated the list, I want to stop using a String of items and instead use key+value pair - user803271 2012-04-04 10:29
Edit your question with your code, so I can better understand what's your proble - Ant4res 2012-04-04 10:48
The following list is using as its datasource a string array called items.

public ArrayList myList = new ArrayList(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 h - user803271 2012-04-04 11:15

Ads