How to combine two array list and show in a listview in android

前端 未结 3 1425
北海茫月
北海茫月 2020-12-03 05:23

I want to combine two ArrayList into a single one.

My first arraylist looks like this:

{a,s,d,f,g,h,......}

My second arraylist lo

3条回答
  •  攒了一身酷
    2020-12-03 05:42

    List a = new ArrayList();
    a.add("bla");
    a.add("bla");
    a.add("bla");
    List b = new ArrayList();
    b.add("Boo");
    b.add("Boo");
    b.add("Boo");
    
    // Append content of a to b
    b.addAll(a);
    
    // New list containing a union b
    List union = new ArrayList(a);
    union.addAll(b);
    

    To show that in a list view, you will need an adapter along with a list view. I recommend you read the Android Developer guide's tutorial about ListView: Hello ListView

    public class HelloListView extends ListActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        List b = new ArrayList();
        b.add("Boo");
        b.add("Boo");
        b.add("Boo");
    
        setListAdapter(new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, b));
      }
    }
    

提交回复
热议问题