0

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?

1
  • 1
    This is not related to your question, but I think you might want to watch this — Google I/O 2010 - The world of ListView. Your getView() is not optimized… Commented Jul 20, 2012 at 17:56

4 Answers 4

5

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.

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

9 Comments

This looks like a good method. Though it says the constructor is not defined.
Hmm, could you edit what you are trying into your question? Also I think R.layout.item_row_layout is the layout you want. I didn't see the different layouts before in your question.
Yes that was a typo, the layout has been fixed.
I'm still confused as to why the constructor is not defined, could you post how and where you are creating the SimpleAdapter?
I posted exactly what you have there -- with the correct layout of course, and declared adapter to SimpleAdapter instead of ItemAdapter -- or was that the problem?
|
3

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

Comments

2

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.

7 Comments

Do this in the Activity I assume and not the Adapter?
@KickingLettuce you could do it in either place, but if you do it in the Activity then you won't have to alter the Adapter constructor.
You'd better do it on your Activity, yes.
let say both have 20 items in a list. does this now make 40 items? List position is important because I am setting TextViews based on the List position of 0 -20, and 0 - 20. (because the lists are related) Position 1 of one list related to position 1 of the other)
Yes, the resulting List will have the sum of both lists.
|
2

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

2 Comments

Can you speak more of the first option and put into code? Assuming its just couple of lines like I think...
I didn't actually use this method, but it was the closest answer to what I did and got me on the right path. All is well and working now! Thank you again. Voting complete.

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.