How to combine two array list and show in a listview in android
我想把两个数组组合成一个。
我的第一个数组列表如下:
1 | {a,s,d,f,g,h,......} |
我的第二个数组列表如下:
1 | {z,x,c,v,b,.....} |
号
然后我想把两者结合起来
1 | {a,s,d,f,g,h,.....,z,x,c,v,b.....} |
第一个列表是
1 | ArrayList<String> firstname1 = new ArrayList<String>(); |
。
其中第二个列表是
1 | ArrayList<String> first = new ArrayList<String>(); |
现在我想把所有这些结合在一起,我想把它们列在列表视图中。怎么做?
将两个数组列表合并为一个数组列表
1 | firstname1.addAll(first); |
有关concat两个列表的示例代码,请参阅本文。
如何在列表视图中显示这些项目:
您的布局应该是(正如我使用的main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_marginTop="0dip" android:background="@android:color/transparent"> <ListView android:id="@+id/custom_list_view" android:layout_width="fill_parent" android:layout_height="fill_parent" android:cacheColorHint="#00000000" android:fastScrollEnabled="true" android:background="@android:color/transparent" android:fadeScrollbars="true" android:layout_gravity="top" android:padding="2dp"> </ListView> </LinearLayout> |
。
现在以
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | public class CustomListView extends Activity { ArrayList<String> firstname1; ArrayList<String> first; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); firstname1 = new ArrayList<String>(); first = new ArrayList<String>(); //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<String>(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() } } |
快乐的编码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | List<String> a = new ArrayList<String>(); a.add("bla"); a.add("bla"); a.add("bla"); List<String> b = new ArrayList<String>(); 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<String> union = new ArrayList<String>(a); union.addAll(b); |
要在列表视图中显示这一点,您需要一个适配器和一个列表视图。我建议您阅读Android开发者指南中有关ListView的教程:Hello ListView
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public class HelloListView extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); List<String> b = new ArrayList<String>(); b.add("Boo"); b.add("Boo"); b.add("Boo"); setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, b)); } } |
号