Android中ListView控件的简单使用

在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>

注意:[color=red]ListView的id要使用android内置的id:@android:id/list[/color]
/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>

注意:[color=red]对于Button,CheckBox等按钮控件如果用在ListView中需要设置android:clickable="false" android:focusable="false" android:focusableInTouchMode="false",否则的话click事件将被这些控件捕获,无法被ListView的OnListItemClick捕获;[/color]
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 {
@Override
public 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;
}

@Override
public 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的处理方式在上面的代码中给出;

运行结果如下:
[img]http://dl.iteye.com/upload/attachment/0070/3574/594dd9a0-913f-33e8-b85c-6f2450caf55d.jpg[/img]
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值