Passing Multiple Lists into ArrayAdapter

后端 未结 4 1824
隐瞒了意图╮
隐瞒了意图╮ 2021-01-17 00:59

I start of with this in Activity:

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

Now here is ItemAd

相关标签:
4条回答
  • 2021-01-17 01:46

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-17 01:59

    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.

    0 讨论(0)
  • 2021-01-17 02:04

    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;
          }
      }
    
    0 讨论(0)
提交回复
热议问题