user803271
user803271

Reputation: 115

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

Upvotes: 0

Views: 564

Answers (1)

Ant4res
Ant4res

Reputation: 1225

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);

Upvotes: 1

Related Questions