如何两个数组列表,并显示相结合在Android的列表视图 [英] How to combine two array list and show in a listview in android
问题描述
欲2的ArrayList组合成一个单一的。
I want to combine two ArrayList into a single one.
我的第一个ArrayList的是这样的:
My first arraylist looks like this:
{a,s,d,f,g,h,......}
我的第二个数组列表看起来是这样的:
My second arraylist looks like this:
{z,x,c,v,b,.....}
然后我想这两个结合起来,是因为
Then I want to combine both to be as
{a,s,d,f,g,h,.....,z,x,c,v,b.....}
第一清单中的
First List is
ArrayList<String> firstname1 = new ArrayList<String>();
凡为第二个列表是为
Where as the second list is as
ArrayList<String> first = new ArrayList<String>();
现在我想这一切结合在一起,我希望它在列表视图中列了出来。 如何做到这一点?
Now I want to combine all this together and I want it to be listed out in list view. How to do this?
推荐答案
将两个的ArrayList到一个单一的一个的
Combine two ArrayList into a single one
firstname1.addAll(first);
请参阅这篇文章样品code到Concat的两个列表。
Please refer this article for sample code to concat two lists.
如何显示在列表中查看这些项目:的
How to show these items in list view :
你的布局应该是(为我所用的main.xml)的
<?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>
现在活动为 CustomListView.java
的
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()
}
}
快乐编码。
这篇关于如何两个数组列表,并显示相结合在Android的列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!