Siv*_*a K 20 android arraylist
我想将两个ArrayList合并为一个.
我的第一个arraylist看起来像这样:
{a,s,d,f,g,h,......}
Run Code Online (Sandbox Code Playgroud)
我的第二个arraylist看起来像这样:
{z,x,c,v,b,.....}
Run Code Online (Sandbox Code Playgroud)
然后我想把两者结合起来
{a,s,d,f,g,h,.....,z,x,c,v,b.....}
Run Code Online (Sandbox Code Playgroud)
第一个清单是
ArrayList<String> firstname1 = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
第二个列表的位置为
ArrayList<String> first = new ArrayList<String>();
Run Code Online (Sandbox Code Playgroud)
现在我想将所有这些组合在一起,我希望它在列表视图中列出.这该怎么做?
Pan*_*mar 50
将两个ArrayList合并为一个
firstname1.addAll(first);
Run Code Online (Sandbox Code Playgroud)
请参阅此文章以获取连接两个列表的示例代码.
如何在列表视图中显示这些项:
你的布局应该是(因为我使用的是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>
Run Code Online (Sandbox Code Playgroud)
现在活动为 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()
}
}
Run Code Online (Sandbox Code Playgroud)
快乐的编码.
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);
Run Code Online (Sandbox Code Playgroud)
要在列表视图中显示,您需要一个适配器以及列表视图.我建议您阅读有关ListView:Hello ListView的Android开发人员指南的教程
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));
}
}
Run Code Online (Sandbox Code Playgroud)