42.Android之ListView中ArrayAdapter简单学习

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

改下布局文件:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical">
 6 
 7     <ListView
 8         android:id="@+id/lv1"
 9         android:layout_width="match_parent"
10         android:layout_height="wrap_content" 
11         android:layout_weight="1">
12     </ListView>
13     
14      <ListView
15         android:id="@+id/lv2"
16         android:layout_width="match_parent"
17         android:layout_height="wrap_content" 
18         android:layout_weight="1">
19     </ListView>
20     
21       <ListView
22         android:id="@+id/lv3"
23         android:layout_width="match_parent"
24         android:layout_height="wrap_content"
25         android:layout_weight="1" >
26     </ListView>
27      
28         <ListView
29         android:id="@+id/lv4"
30         android:layout_width="match_parent"
31         android:layout_height="wrap_content"
32         android:layout_weight="1" >
33     </ListView>
34 </LinearLayout>

代码修改:

 1 package com.example.listviewarraydemo;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.widget.ArrayAdapter;
 6 import android.widget.ListView;
 7 
 8 
 9 public class MainActivity extends Activity {
10 
11     private ListView listView = null;
12     private ListView listView_two = null;
13     private ListView listView_three = null;
14     private ListView listView_four = null;
15     ArrayAdapter<String> adapter1;
16     ArrayAdapter<String> adapter2;
17     ArrayAdapter<String> adapter3;
18     ArrayAdapter<String> adapter4;
19     private static final String[] strs = {"one item","two item","three item","four item"};
20     private static final String[] strs_two = {"1111","2222","3333","4444"};
21     private static final String[] strs_three = {"AAAA","BBBB","CCCC","DDDD"};
22     private static final String[] strs_four = {"yuwen","shuxue","yingyu","tiyu"};
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         
28         listView = (ListView)findViewById(R.id.lv1);
29         listView_two = (ListView)findViewById(R.id.lv2);
30         listView_three = (ListView)findViewById(R.id.lv3);
31         listView_four = (ListView)findViewById(R.id.lv4);
32         
33         adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,strs);
34         adapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_checked,strs_two);
35         adapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,strs_three);
36         adapter4 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_single_choice,strs_four);
37         
38         listView.setAdapter(adapter1);
39         listView_two.setAdapter(adapter2);
40         listView_two.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
41         listView_three.setAdapter(adapter3);
42         listView_three.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
43         listView_four.setAdapter(adapter4);
44         listView_four.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
45     }
46      
47 }

一些代码说明:

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

运行效果:

 

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

posted @ 2016-03-14 19:17  chaoer  阅读(350)  评论(0编辑  收藏  举报