1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
2.item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18dip" />
<CheckBox
android:id="@+id/cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:clickable="false"
android:focusable="false"
android:focusableInTouchMode="false" />
</LinearLayout>
3.Adapter
package com.example.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private List<Map<String, Object>> mData;
public static Map<Integer, Boolean> isSelected;
public MyAdapter(Context context) {
mInflater = LayoutInflater.from(context);
init();
}
//初始化
private void init() {
mData = new ArrayList<Map<String,Object>>();
for (int i=0; i<5; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("img", R.drawable.ic_launcher);
map.put("title", "第" + (i+1) + "行的标题");
mData.add(map);
}
//定义isSelected这个map是记录每个listItem的状态,初始状态全部为false
isSelected = new HashMap<Integer, Boolean>();
for (int i=0; i<mData.size(); i++) {
isSelected.put(i, false);
}
}
public int getCount() {
// TODO Auto-generated method stub
return mData.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
//convertView为null的时候初始化convertView
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.img = (ImageView)convertView.findViewById(R.id.img);
holder.title = (TextView)convertView.findViewById(R.id.title);
holder.cBox = (CheckBox)convertView.findViewById(R.id.cb);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.img.setBackgroundResource((Integer)mData.get(position).get("img"));
holder.title.setText(mData.get(position).get("title").toString());
holder.cBox.setChecked(isSelected.get(position));
return convertView;
}
public final class ViewHolder {
public ImageView img;
public TextView title;
public CheckBox cBox;
}
}
4.MainActivity
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.example.test.MyAdapter.ViewHolder;
/**
* 总结:
* 1、数据可以在Activity中获取,在初始化Adapter的时候当做参数传递过去,这样就可以是list编程动态的。
* 2、对MyAdapter中getview的理解不够,尤其是convertView.setTag(holder),
* @author Administrator
*/
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView list = (ListView)findViewById(R.id.lv);
MyAdapter adapter = new MyAdapter(this);
list.setAdapter(adapter);
list.setItemsCanFocus(false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
ViewHolder vHolder = (ViewHolder)view.getTag();
//在每次获取点击的item时将对应的checkBox状态改变,同时修改map的值
vHolder.cBox.toggle();
MyAdapter.isSelected.put(position, vHolder.cBox.isChecked());
}
});
}
}