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

前端 未结 3 1426
北海茫月
北海茫月 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:25

    Combine two ArrayList into a single one

    firstname1.addAll(first);
    

    Please refer this article for sample code to concat two lists.

    How to show these items in list view :

    your layout should be ( as I used main.xml)

    
    
        
        
     
    

    and now Activity as CustomListView.java

    public class CustomListView extends Activity {
    
        ArrayList firstname1;
        ArrayList first;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            firstname1 = new ArrayList();
            first = new ArrayList();
            //Let both array list having some data
    
            firstname1.add("firstname1_data1");
            firstname1.add("firstname1_data2");
            firstname1.add("firstname1_data3");
            firstname1.add("firstname1_data4");
            firstname1.add("firstname1_data5");
            firstname1.add("firstname1_data6");
            firstname1.add("firstname1_data7");
            firstname1.add("firstname1_data8");
            firstname1.add("firstname1_data9");
            firstname1.add("firstname1_data10");
    
            first.add("first_data1");
            first.add("first_data2");
            first.add("first_data3");
            first.add("first_data4");
            first.add("first_data5");
            first.add("first_data6");
            first.add("first_data7");
            first.add("first_data8");
            first.add("first_data9");
            first.add("first_data10");
    
            //Now copying value of first to firstname, as your requirement
            //Please refer http://www.java-examples.com/append-all-elements-other-collection-java-arraylist-example for sample code to concat two lists.
            firstname1.addAll(first);
    
            //Lets show your data into list view
    
            // Get a handle to the list view
            ListView lv = (ListView) findViewById(R.id.custom_list_view);
    
            lv.setAdapter(new ArrayAdapter(CustomListView.this,
                    android.R.layout.simple_list_item_1, firstname1));
            //Please refer http://developer.android.com/reference/android/widget/ListView.html for details of setAdapter()
        }
    }
    

    Happy coding.

提交回复
热议问题