2

I have an ArrayList<HashMap<Contact, Name>> and I want to populate a ListView with it. What type of adapter can I use and what do I put in the from field of my adapter? Example below:

String[] from = ?
int[] to = new int[] { R.id.contact, R.id.name});
adapter = new KindOfAdapter(this, R.layout.row, from, to)

Hope it is clear. Appreciate any help.

0

2 Answers 2

2

1. Implement BaseAdapter

I think your best bet is to extend BaseAdapter and implement the following methods:

getCount()
getItem(int)
getItemId(int)
getView(int, View, ViewGroup)

It would look something like this:

public class MyAdapter extends BaseAdapter() {
   private List<Map<Contact, Name>> map;
   private Context context;

   public MyAdapter(List<Map<Contact, Name>> map>, Context context) {
      this.map = map;
      this.context = context;
   }

   public int getCount() {
      return map.size(); // or do you want one list item per name?
      // if so, just count out all the map entries in each item of the list
   }

   public int getItemId(int position) {
      return position; // doesn't matter too much...
   }

   public View getView(int position, View convertView, ViewGroup parent) {
      // populate the view here...
      // use LayoutInflater.from(context).inflate(resource, parent, false) to inflate new views
   }
}

2. Be scrupulous about using the ViewHolder pattern

When implementing getView(), utilizing this design pattern will save a LOT of memory:

http://www.screaming-penguin.com/node/7767

Sign up to request clarification or add additional context in comments.

2 Comments

This kind of makes sense, but I'm trying to understand the population process. How does getView() get the data for each row from my HashMap? And shouldn't I also implement newView()? Thanks for the help!
Look at the position argument passed in to the getView() method. You can directly access the data off of the stored List then. You do not need to implement newView().
0

An ArrayAdapter> - ArrayAdapter is designed to be used with ArrayLists (and arrays[]), although it doesn't support View binding you only have to override getView()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.