main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_centerInParent="true" /> </RelativeLayout>
listviewitem.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="70dip" > <TextView android:id="@+id/textView" android:layout_marginLeft="80dip" android:layout_width="wrap_content" android:layout_height="50dip" /> <CheckBox android:id="@+id/checkbox" android:layout_marginLeft="90dip" android:layout_width="wrap_content" android:layout_height="50dip" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" /> </LinearLayout>
ListViewAdapter如下:
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.CheckBox; import android.widget.SimpleAdapter; import android.widget.TextView; public class ListViewAdapter extends SimpleAdapter { private List<? extends Map<String, ?>> mArrayList; private int resource; private LayoutInflater mLayoutInflater; public ListViewAdapter(Context context,List<? extends Map<String, ?>> data, int resource, String[] from,int[] to) { super(context, data, resource, from, to); this.mArrayList=data; this.resource=resource; mLayoutInflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE); } @Override public int getCount() { if (mArrayList==null) { return 0; } else { return mArrayList.size(); } } @Override public Object getItem(int position) { if (mArrayList==null) { return null; } else { return mArrayList.get(position); } } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder=null; if(convertView==null){ viewHolder=new ViewHolder(); convertView=mLayoutInflater.inflate(resource, null); viewHolder.textView=(TextView) convertView.findViewById(R.id.textView); viewHolder.checkBox=(CheckBox) convertView.findViewById(R.id.checkbox);; convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder) convertView.getTag(); } if (mArrayList!=null) { if (viewHolder.textView!=null) { viewHolder.textView.setText((String)(mArrayList.get(position).get("content"))); } if (viewHolder.checkBox!=null) { boolean isChecked=MainActivity.mCheckBoxStatusHashMap.get(position); viewHolder.checkBox.setChecked(isChecked); } } return convertView; } private class ViewHolder{ TextView textView; CheckBox checkBox; } }
MainActivity如下:
import java.util.ArrayList; import java.util.HashMap; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.CheckBox; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import android.app.Activity; /** * Demo描述: * ListView的Item中含有CheckBox的处理方法 * */ public class MainActivity extends Activity { private ListView mListView; private HashMap<String, Object> mHashMap; private ArrayList<HashMap<String, Object>> mArrayList; public static HashMap<Integer, Boolean> mCheckBoxStatusHashMap=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mCheckBoxStatusHashMap=new HashMap<Integer, Boolean>(); mArrayList=new ArrayList<HashMap<String,Object>>(); mListView=(ListView) findViewById(R.id.listview); addDataForListView(); mListView.setAdapter (new ListViewAdapter(MainActivity.this, mArrayList, R.layout.listviewitem, new String []{"content"}, new int []{R.id.textView})); mListView.setOnItemClickListener(new ItemClickListenerImpl()); } private void addDataForListView(){ for (int i = 0; i < 20; i++) { mHashMap=new HashMap<String, Object>(); mHashMap.put("content", ""+i); mArrayList.add(mHashMap); mCheckBoxStatusHashMap.put(i, false); } } private class ItemClickListenerImpl implements OnItemClickListener { @Override public void onItemClick(AdapterView<?> parent, View view, int position,long arg) { ListView lv = (ListView) parent; CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkbox); checkBox.toggle(); boolean itemIsCheck = checkBox.isChecked(); HashMap<String, Object> itemHashMap = (HashMap<String, Object>) lv.getItemAtPosition(position); mCheckBoxStatusHashMap.put(position, itemIsCheck); } } }