42.Android之ListView中ArrayAdapter简单学习

时间:2022-09-05 07:33:23

今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码。

改下布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <ListView
android:id="@+id/lv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView> <ListView
android:id="@+id/lv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</ListView> <ListView
android:id="@+id/lv3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView> <ListView
android:id="@+id/lv4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</ListView>
</LinearLayout>

代码修改:

 package com.example.listviewarraydemo;

 import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; public class MainActivity extends Activity { private ListView listView = null;
private ListView listView_two = null;
private ListView listView_three = null;
private ListView listView_four = null;
ArrayAdapter<String> adapter1;
ArrayAdapter<String> adapter2;
ArrayAdapter<String> adapter3;
ArrayAdapter<String> adapter4;
private static final String[] strs = {"one item","two item","three item","four item"};
private static final String[] strs_two = {"1111","2222","3333","4444"};
private static final String[] strs_three = {"AAAA","BBBB","CCCC","DDDD"};
private static final String[] strs_four = {"yuwen","shuxue","yingyu","tiyu"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); listView = (ListView)findViewById(R.id.lv1);
listView_two = (ListView)findViewById(R.id.lv2);
listView_three = (ListView)findViewById(R.id.lv3);
listView_four = (ListView)findViewById(R.id.lv4); adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strs);
adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,strs_two);
adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,strs_three);
adapter4 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,strs_four); listView.setAdapter(adapter1);
listView_two.setAdapter(adapter2);
listView_two.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView_three.setAdapter(adapter3);
listView_three.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
listView_four.setAdapter(adapter4);
listView_four.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
} }

一些代码说明:

ListView自身带了单选、多选模式,可通过listview.setChoiceMode来设置:
listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//开启多选模式
listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);//开启单选模式

运行效果:

42.Android之ListView中ArrayAdapter简单学习

42.Android之ListView中ArrayAdapter简单学习

至于ListView与SimpleAdapter使用可以参考这里http://www.cnblogs.com/benchao/p/5213005.html