弓背霞明剑照霜,秋风走马出咸阳。这篇文章主要讲述Android ListVeiw控件(转载整理)相关的知识,希望能为你提供帮助。
![Android ListVeiw控件(转载整理)](http://img.readke.com/220417/16105I344-0.jpg)
文章图片
列表的显示需要三个元素:
1.ListVeiw 用来展示列表的View。
2.适配器 用来把数据映射到ListView上的中介。
3.数据 具体的将被映射的字符串,图片,或者基本组件。
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter
其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来。
我们从最简单的ListView开始:
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 | /**
* @author allin
*
*/
public class MyListView extends Activity {
private ListView listView;
//private List<
String>
data = https://www.songbingjia.com/android/new ArrayList<
String>
();
@Override
public void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
listView = new ListView( this );
listView.setAdapter( new ArrayAdapter<
String>
( this , android.R.layout.simple_expandable_list_item_1,getData()));
setContentView(listView);
}
private List<
String>
getData(){
List<
String>
data = https://www.songbingjia.com/android/ new ArrayList<
String>
();
data.add( "测试数据1" );
data.add( "测试数据2" );
data.add( "测试数据3" );
data.add( "测试数据4" );
return data;
}
} |
![Android ListVeiw控件(转载整理)](http://img.readke.com/220417/16105G539-1.jpg)
文章图片
SimpleCursorAdapter
sdk的解释是这样的:An easy adapter to map columns from a cursor to TextViews or ImageViews defined in an XML file. You can specify which columns you want, which views you want to display the columns, and the XML file that defines the appearance of these views。简单的说就是方便把从游标得到的数据进行列表显示,并可以把指定的列映射到对应的TextView中。
下面的程序是从电话簿中把联系人显示到类表中。先在通讯录中添加一个联系人作为数据库的数据。然后获得一个指向数据库的Cursor并且定义一个布局文件(当然也可以使用系统自带的)。
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 | /**
* @author allin
*
*/
public class MyListView2 extends Activity {
private ListView listView;
//private List<
String>
data = https://www.songbingjia.com/android/new ArrayList<
String>
();
@Override
public void onCreate(Bundle savedInstanceState){
super .onCreate(savedInstanceState);
listView = new ListView( this );
Cursor cursor = getContentResolver().query(People.CONTENT_URI, null , null , null , null );
startManagingCursor(cursor);
ListAdapter listAdapter = new SimpleCursorAdapter( this , android.R.layout.simple_expandable_list_item_1,
cursor,
new String[]{People.NAME},
new int []{android.R.id.text1});
listView.setAdapter(listAdapter);
setContentView(listView);
}
} |
startManagingCursor(cursor); 我们将获得的Cursor对象交由Activity管理,这样Cursor的生命周期和Activity便能够自动同步,省去自己手动管理Cursor。
SimpleCursorAdapter 构造函数前面3个参数和ArrayAdapter是一样的,最后两个参数:一个包含数据库的列的String型数组,一个包含布局文件中对应组件id的int型数组。其作用是自动的将String型数组所表示的每一列数据映射到布局文件对应id的组件上。上面的代码,将NAME列的数据一次映射到布局文件的id为text1的组件上。
注意:需要在AndroidManifest.xml中如权限:< uses-permission android:name="android.permission.READ_CONTACTS"> < /uses-permission>
运行后效果如下图:
![Android ListVeiw控件(转载整理)](http://img.readke.com/220417/16105M096-2.jpg)
文章图片
SimpleAdapter
simpleAdapter的扩展性最好,可以定义各种各样的布局出来,可以放上ImageView(图片),还可以放上Button(按钮),CheckBox(复选框)等等。下面的代码都直接继承了ListActivity,ListActivity和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。
下面的程序是实现一个带有图片的类表。
首先需要定义好一个用来显示每一个列内容的xml
vlist.xml
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 | <
? xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<
ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<
LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<
TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22px" />
<
TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px" />
<
/ LinearLayout >
<
/ 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 | /**
* @author allin
*
*/
public class MyListView3 extends ListActivity {
// private List<
String>
data = https://www.songbingjia.com/android/new ArrayList<
String>
();
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter( this ,getData(),R.layout.vlist,
new String[]{ "title" , "info" , "img" },
new int []{R.id.title,R.id.info,R.id.img});
setListAdapter(adapter);
}
private List<
Map<
String, Object>
>
getData() {
List<
Map<
String, Object>
>
list = new ArrayList<
Map<
String, Object>
>
();
Map<
String, Object>
map = new HashMap<
String, Object>
();
map.put( "title" , "G1" );
map.put( "info" , "google 1" );
map.put( "img" , R.drawable.i1);
list.add(map);
map = new HashMap<
String, Object>
();
map.put( "title" , "G2" );
map.put( "info" , "google 2" );
map.put( "img" , R.drawable.i2);
list.add(map);
map = new HashMap<
String, Object>
();
map.put( "title" , "G3" );
map.put( "info" , "google 3" );
map.put( "img" , R.drawable.i3);
list.add(map);
return list;
}
} |
运行效果如下图:
![Android ListVeiw控件(转载整理)](http://img.readke.com/220417/16105L051-3.jpg)
文章图片
有按钮的ListView
但是有时候,列表不光会用来做显示用,我们同样可以在在上面添加按钮。添加按钮首先要写一个有按钮的xml文件,然后自然会想到用上面的方法定义一个适配器,然后将数据映射到布局文件上。但是事实并非这样,因为按钮是无法映射的,即使你成功的用布局文件显示出了按钮也无法添加按钮的响应,这时就要研究一下ListView是如何现实的了,而且必须要重写一个类继承BaseAdapter。下面的示例将显示一个按钮和一个图片,两行字如果单击按钮将删除此按钮的所在行。并告诉你ListView究竟是如何工作的。效果如下:
![Android ListVeiw控件(转载整理)](http://img.readke.com/220417/16105I940-4.jpg)
文章图片
vlist2.xml
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 | <
? xml version="1.0" encoding="utf-8"?>
<
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<
ImageView android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
<
LinearLayout android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<
TextView android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="22px" />
<
TextView android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFFFF"
android:textSize="13px" />
<
/ LinearLayout >
<
Button android:id="@+id/view_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/s_view_btn"
android:layout_gravity="bottom|right" />
<
/ 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | /**
* @author allin
*
*/
public class MyListView4 extends ListActivity {
private List<
Map<
String, Object>
>
mData;
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
mData = https://www.songbingjia.com/android/getData();
MyAdapter adapter = new MyAdapter( this );
setListAdapter(adapter);
}
private List<
Map<
String, Object>
>
getData() {
List<
Map<
String, Object>
>
list = new ArrayList<
Map<
String, Object>
>
();
Map<
String, Object>
map = new HashMap<
String, Object>
();
map.put( "title" , "G1" );
map.put( "info" , "google 1" );
map.put( "img" , R.drawable.i1);
list.add(map);
map = new HashMap<
String, Object>
();
map.put( "title" , "G2" );
map.put( "info" , "google 2" );
map.put( "img" , R.drawable.i2);
list.add(map);
map = new HashMap<
String, Object>
();
map.put( "title" , "G3" );
map.put( "info" , "google 3" );
map.put( "img" , R.drawable.i3);
list.add(map);
return list;
}
// ListView 中某项被选中后的逻辑
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Log.v( "MyListView4-click" , (String)mData.get(position).get( "title" ));
}
/**
* listview中点击按键弹出对话框
*/
public void showInfo(){
new AlertDialog.Builder( this )
.setTitle( "我的listview" )
.setMessage( "介绍..." )
.setPositiveButton( "确定" , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
&n |