在android中ListView是一个经常使用到的控件,该控件是android众多列表控件中的一种,以垂直的方式显示一组项,对于ListView的使用包含以下三部分:
1)建立一个包含ListView的布局文件和一个针对ListView中每一个项的布局文件;
2)创建一个Activity(最简单的方式是继承ListActivity);
3)创建一个ListAdapter,填充所需的数据后通过addListAdapter添加ListAdapter至该Activity;
在下面的代码中给出的只是最简单的方式用以演示最基本的使用方式:
1)建立所需的布局文件:
/res/layout/listview.xml
<?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="@android:id/list"        android:layout_width="fill_parent"        android:layout_height="wrap_content" /></LinearLayout>

注意: ListView的id要使用android内置的id:@android:id/list
/res/layout/listview_item.xml
<?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="horizontal" >    <CheckBox        android:id="@+id/checked"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:clickable="false"        android:focusable="false"        android:focusableInTouchMode="false" />    <TextView        android:id="@+id/author"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#00FFFF"        android:textStyle="bold"        android:textSize="20dp" />    <TextView        android:id="@+id/title"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="#FFFF00"        android:textSize="15dp" /></LinearLayout>

注意: 对于Button,CheckBox等按钮控件如果用在ListView中需要设置android:clickable="false" android:focusable="false" android:focusableInTouchMode="false",否则的话click事件将被这些控件捕获,无法被ListView的OnListItemClick捕获;
2)创建一个继承自ListActivity的活动:
package ui.app;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.ListActivity;import android.os.Bundle;import android.view.View;import android.widget.CheckBox;import android.widget.LinearLayout;import android.widget.ListView;import android.widget.SimpleAdapter;public class ListViewActivity extends ListActivity {@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.listview);SimpleAdapter adapter = new SimpleAdapter(this, getData(),R.layout.listview_item, new String[] { "author", "title" },new int[] { R.id.author, R.id.title });setListAdapter(adapter);}private List<Map<String, String>> getData() {List<Map<String, String>> data = new ArrayList<Map<String, String>>();Map<String, String> entry = new HashMap<String, String>();entry.put("author", "哈迪斯");entry.put("title", "死神");data.add(entry);entry = new HashMap<String, String>();entry.put("author", "宙斯");entry.put("title", "神王.雷神");data.add(entry);entry = new HashMap<String, String>();entry.put("author", "波塞冬");entry.put("title", "海神");data.add(entry);return data;}@Overridepublic void onListItemClick(ListView list, View view, int position, long id) {LinearLayout layout = (LinearLayout) list.getChildAt(position);CheckBox cb = (CheckBox) layout.findViewById(R.id.checked);if (cb.isChecked()) {cb.setChecked(false);} else {cb.setChecked(true);}}}

3)关联Adapter:
关联ListAdapter和设置onListItemClick的处理方式在上面的代码中给出;

运行结果如下:

更多相关文章

  1. NPM 和webpack 的基础使用
  2. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  3. 读取android手机流量信息
  4. 三、安卓UI学习(1)
  5. android 使用html5作布局文件: webview跟javascript交互
  6. Android(安卓)多媒体扫描过程(Android(安卓)Media Scanner Proces
  7. android“设置”里的版本号
  8. android用户界面之按钮(Button)教程实例汇
  9. Android开发环境搭建

随机推荐

  1. android 读取通讯录
  2. Android全屏和强制横屏竖屏设置
  3. android cupcake源码编译问题
  4. android获取联系人所有内容
  5. android RadioGroup设置某一个被选中
  6. Android webview 自动填值和提交
  7. Android 在线升级APK
  8. android 网络下载获取文件大小
  9. 背景及边框处理
  10. PhoneWindowManager