ListView的Item含有CheckBox时的处理

简介: main.xml如下:   listviewitem.xml如下: ListViewAdapter如下: import java.

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);
		}
	}
}


 

相关文章
|
存储 C++ 开发者
QListWidget和QListView的使用和item点击事件
QListWidget和QListView的使用和item点击事件
ListView Item多布局的实现
ListView这个小节的最后一节,给大家带来的是ListView多布局Item的实现, 何为ListView Item多布局,打个比方,QQ这种聊天列表
117 0
|
索引
Android--listView中的button控件获取item的值
版权声明:本文为博主原创文章,转载请标明出处。 https://blog.csdn.net/chaoyu168/article/details/55253877 在listv...
1210 0
|
Android开发
Gridview的item含有checkbox,setOnItemClickListener方法失效的问题
在开发中我们常常遇到一些莫名奇妙的问题,就比如Gridview的item含有checkbox,setOnItemClickListener方法失效的问题。 刚开始网上搜了一下,如http://my.oschina.net/fuckboogie/blog/346202 按这个没办法解决我上面的问题,于是我在item的子布局里面新增了 &lt;span style="font-size
895 0