Android Listview与Checkbox结合实现选择,删除,显示选择结果

 

网上有很多Listview与Checbox结合的例子,大多都存在这样那样的问题,要么选择异常,要么滚动Listview后选择消失,要么删除异常,要么无法正常显示选择结果。

以下为真正无误的,实用的Listview与Checkbox结合实现选择,删除,显示选择结果。

Android Listview与Checkbox结合实现选择,删除,显示选择结果_第1张图片

MainActivity.java

package com.example.listviewcheckbox;

import java.util.ArrayList;
import java.util.List;

import com.example.listviewcheckbox.R;

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.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	Button show;
	Button select;
	Button deselect;
	ListView lv;
	Context mContext;
	MyListAdapter adapter;

	List selected = new ArrayList();
	private List items;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //mContext = getApplicationContext();
        mContext = MainActivity.this;
        select = (Button)findViewById(R.id.select);
        deselect = (Button)findViewById(R.id.deselect);
        show = (Button)findViewById(R.id.show);
        lv = (ListView)findViewById(R.id.lv);
        
        items = new ArrayList();
        
        for(int i=0;i< 50;i++){
    		Item item = new Item();
    	    item.name = "wxz"+i;
    		item.address = "ZhengZhou";
    		item.checked = false;
    		items.add(item);
      }
        
        adapter = new MyListAdapter(items);
        lv.setAdapter(adapter);
        
        lv.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener(){

			@Override
			public void onItemClick(AdapterView parent, View view, int position,
					long id) {
				// TODO Auto-generated method stub
				items.get(position).checked = !items.get(position).checked;
				adapter.notifyDataSetChanged();	 
				Toast.makeText(mContext, "单击:"+items.get(position).name +",id:"+id, Toast.LENGTH_SHORT).show();
				
			}
        	
        });
        
        select.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				int k = items.size();
				for(int i=0; i items;
        public MyListAdapter(List items){
    		this.items = items;
    		
		inflater = LayoutInflater.from(mContext);
			
        }
        @Override
		public int getCount() {
			// 返回值控制该Adapter将会显示多少个列表项
	        return items == null ? 0 : items.size();
		}

		@Override
		public Object getItem(int position) {
			// 返回值决定第position处的列表项的内容
			return items.get(position);
		}

		@Override
		public long getItemId(int position) {
			// 返回值决定第position处的列表项的ID
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
		    ViewHolder holder = null;
			Item item = items.get(position);
		    if (convertView == null) {
				Log.e("MainActivity","position1 = "+position);
				convertView = inflater.inflate(R.layout.listitem, null);
				
				holder = new ViewHolder();
				holder.btnDel = (Button)convertView.findViewById(R.id.btnDel);
				holder.cbCheck = (CheckBox)convertView.findViewById(R.id.cbCheck);
				holder.tvName = (TextView)convertView.findViewById(R.id.tvName);
				holder.tvAddress = (TextView)convertView.findViewById(R.id.tvAddress);
				holder.tvName.setText(item.name);
				holder.tvAddress.setText(item.address);
				
			    convertView.setTag(holder);
			    
			}else{
				Log.e("MainActivity","position2 = "+position);
				holder = (ViewHolder)convertView.getTag();
                holder.cbCheck.setChecked(item.checked);
                holder.tvName.setText(item.name);
                holder.tvAddress.setText(item.address);
              
			}
		    holder.btnDel.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					  //删除list中的数据
                    items.remove(position);
                    //通知列表数据修改
                    adapter.notifyDataSetChanged();
				}
		    	
		    });
			holder.cbCheck.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					CheckBox cb = (CheckBox)v;
					items.get(position).checked = cb.isChecked(); 
		
				}
			});
		      return convertView;  
		}
    	
    }
    
    static class ViewHolder{
    	public CheckBox cbCheck;
    	public TextView tvName;
    	public TextView tvAddress;
    	public Button btnDel;
    	
    }
    
    class Item {
        private String name;
        private String address;
        private Boolean checked;
    }
}                                                                                                                                                                                            

 列表项listitem.xml




    

    

    

    

    

main.xml



    
        

源码:http://download.csdn.net/detail/xinzheng_wang/5973407

你可能感兴趣的:(Android)