TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Passing Multiple Lists<String> into ArrayAdapter

I start of with this in Activity:

adapter = new ItemAdapter(Items.this, items, totals);
        setListAdapter(adapter);

Now here is ItemAdapter:

public class ItemAdapter extends ArrayAdapter<String> {

private final List<String> items;
private final List<String> totals;
private final Context context;

public ItemAdapter(Context context, List<String> items,
        List<String> totals) {
    super(context, R.layout.item_row_layout, items);
    this.context = context;
    this.items = items;
    this.totals = totals;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater
            .inflate(R.layout.item_row_layout, parent, false);
    TextView t1 = (TextView) rowView.findViewById(R.id.itemName);
    TextView t2 = (TextView) rowView.findViewById(R.id.itemTotal);

    String s1 = items.get(position);
    t1.setText(s1);

    String s2 = totals.get(position);
    t2.setText(s2);


    return rowView;
}

}

Now I know the problem is with the constructor because till only allow me to pass one List, not two into it. Is there another way to do this?

Upvotes: 0

Views: 17312

Answers (4)

Sahil Jain
Sahil Jain

Reputation: 649

Add one more argument in your constructor-- to accommodate the additional List; however don't pass that additional list in the super call.

Then in the getView method access that second list. Take a look at the code:

  public class NotificationsAdapter extends ArrayAdapter<String> {
      private Context context;
      private List<String> list;
      private List<String> listTimeStamp;      

      public NotificationsAdapter(Context context, List<String> resource,List<String> timeResource) {
          super(context, R.layout.notification_list_item, resource);
          this.context = context;
          this.list = resource;
          this.listTimeStamp= timeResource;
      }      

      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View rowView = inflater.inflate(R.layout.notification_list_item, parent, false);      

          TextView textViewMessage = (TextView) rowView.findViewById(R.id.text_view_notifications_list_item);
          textViewMessage.setText(list.get(position));      

          ImageView imageView = (ImageView) rowView.findViewById(R.id.logo_notifications_list_item);
          imageView.setImageResource(R.drawable.iconsmall);      

          TextView textViewTimeStamp = (TextView) rowView.findViewById(R.id.time_stamp_notifications_list_item);
          textViewTimeStamp.setText(listTimeStamp.get(position));      

          return rowView;
      }
  }

Upvotes: 3

Sam
Sam

Reputation: 86948

I suggests using a SimpleAdapter. Here is how to convert your Lists into one List< Map<...>> (but I recommend just building one List< Map<...>> when you are building the separate lists):

List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map;
int count = items.size();
for(int i = 0; i < count; i++) {
    map = new HashMap<String, String>();
    map.put("name", items.get(i));
    map.put("total", totals.get(i));
    list.add(map);
}

adapter = new SimpleAdapter(this, list, R.layout.item_row_layout, new String[] { "name", "total" }, new int[] { R.id.itemName, R.id.itemTotal });

Now your name and total are automatically displayed in one row in their own Views without having to write a custom adapter.

Upvotes: 5

FoamyGuy
FoamyGuy

Reputation: 46846

One option you have is to alter the constructor to accept a List<List<String>> instead of List<String> Then instead of doing this.items = items; you'll have to iterate through all of the subLists in your parameter and add each element to your local object items.

Another and perhaps more straightforward solution is to merge your mulptiple lists before sending it in to the constructor as a parameter. i.e. you could use a method like List.addAll() like this

List.addAll(anotherListObject);

as many times as you need to make one List that contains all of your items.

And yet another option is to use the MergeAdapter that CommonsGuy has created and graciously open sourced to simplify the process by letting you create multiple adapters and then merging them all into one MergeAdapter instead of worrying about merging the Lists

Upvotes: 2

I&#241;igo
I&#241;igo

Reputation: 12813

Why don't you just join both lists?

List<String> list1 = new ArrayList<String>();
a.add("Item 1");
a.add("Item 2");

List<String> list2 = new ArrayList<String>();
b.add("Item 3");
b.add("Item 4");

// Append content of list2 to list1
list1.addAll(list2);

Then you can create your adapter as usual, with a single List.

Upvotes: 2

Related Questions