[1] 程序结构图如下:
其中Person.java是实体类,MainActivity.java是Activity组件类。listitem.xml是自定义的列表每项布局文件。
[2] listitem.xml布局文件源码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:Android="http://schemas.android.com/apk/res/android"
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <LinearLayout
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:orientation="horizontal"
- Android:descendantFocusability="blocksDescendants">
- <CheckBox
- Android:id="@+id/list.select"
- Android:layout_width="wrap_content"
- Android:layout_height="wrap_content"/>
- <TextView
- Android:id="@+id/list.name"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:layout_weight="1"
- Android:text="Name"
- Android:layout_gravity="center"
- Android:textSize="20dp"
- Android:layout_marginLeft="10dp"/>
- <TextView
- Android:id="@+id/list.address"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:layout_weight="1"
- Android:text="Address"
- Android:layout_gravity="center"
- Android:textSize="20dp"/>
- </LinearLayout>
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- Android:orientation="vertical"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent">
- <Button
- Android:id="@+id/show"
- Android:layout_width="fill_parent"
- Android:layout_height="wrap_content"
- Android:text="Show"/>
- <ListView
- Android:id="@+id/lvperson"
- Android:layout_width="fill_parent"
- Android:layout_height="fill_parent"/>
- </LinearLayout>
- package com.andyidea.bean;
- public class Person {
- private String name;
- private String address;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getAddress() {
- return address;
- }
- public void setAddress(String address) {
- this.address = address;
- }
- }
- package com.andyidea.listview;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import com.andyidea.bean.Person;
- import Android.app.Activity;
- import Android.app.AlertDialog;
- import Android.content.Context;
- import Android.os.Bundle;
- import Android.util.Log;
- import Android.view.LayoutInflater;
- import Android.view.View;
- import Android.view.ViewGroup;
- import Android.widget.BaseAdapter;
- import Android.widget.Button;
- import Android.widget.CheckBox;
- import Android.widget.ListView;
- import Android.widget.TextView;
- public class MainActivity extends Activity {
- Button show;
- ListView lv;
- List<Person> persons = new ArrayList<Person>();
- Context mContext;
- MyListAdapter adapter;
- List<Integer> listItemID = new ArrayList<Integer>();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mContext = getApplicationContext();
- show = (Button)findViewById(R.id.show);
- lv = (ListView)findViewById(R.id.lvperson);
- initPersonData();
- adapter = new MyListAdapter(persons);
- lv.setAdapter(adapter);
- show.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- listItemID.clear();
- for(int i=0;i<adapter.mChecked.size();i++){
- if(adapter.mChecked.get(i)){
- listItemID.add(i);
- }
- }
- if(listItemID.size()==0){
- AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
- builder1.setMessage("没有选中任何记录");
- builder1.show();
- }else{
- StringBuilder sb = new StringBuilder();
- for(int i=0;i<listItemID.size();i++){
- sb.append("ItemID="+listItemID.get(i)+" . ");
- }
- AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
- builder2.setMessage(sb.toString());
- builder2.show();
- }
- }
- });
- }
- /**
- * 模拟数据
- */
- private void initPersonData(){
- Person mPerson;
- for(int i=1;i<=12;i++){
- mPerson = new Person();
- mPerson.setName("Andy"+i);
- mPerson.setAddress("GuangZhou"+i);
- persons.add(mPerson);
- }
- }
- //自定义ListView适配器
- class MyListAdapter extends BaseAdapter{
- List<Boolean> mChecked;
- List<Person> listPerson;
- HashMap<Integer,View> map = new HashMap<Integer,View>();
- public MyListAdapter(List<Person> list){
- listPerson = new ArrayList<Person>();
- listlistPerson = list;
- mChecked = new ArrayList<Boolean>();
- for(int i=0;i<list.size();i++){
- mChecked.add(false);
- }
- }
- @Override
- public int getCount() {
- return listPerson.size();
- }
- @Override
- public Object getItem(int position) {
- return listPerson.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view;
- ViewHolder holder = null;
- if (map.get(position) == null) {
- Log.e("MainActivity","position1 = "+position);
- LayoutInflater mInflater = (LayoutInflater) mContext
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- view = mInflater.inflate(R.layout.listitem, null);
- holder = new ViewHolder();
- holder.selected = (CheckBox)view.findViewById(R.id.list_select);
- holder.name = (TextView)view.findViewById(R.id.list_name);
- holder.address = (TextView)view.findViewById(R.id.list_address);
- final int p = position;
- map.put(position, view);
- holder.selected.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- CheckBox cb = (CheckBox)v;
- mChecked.set(p, cb.isChecked());
- }
- });
- view.setTag(holder);
- }else{
- Log.e("MainActivity","position2 = "+position);
- view = map.get(position);
- holder = (ViewHolder)view.getTag();
- }
- holder.selected.setChecked(mChecked.get(position));
- holder.name.setText(listPerson.get(position).getName());
- holder.address.setText(listPerson.get(position).getAddress());
- return view;
- }
- }
- static class ViewHolder{
- CheckBox selected;
- TextView name;
- TextView address;
- }
- }