ListView 與 CheckBox

標題不怎麼變,原因是大部分代碼都摘自http://blog.csdn.net/suren__123/article/details/7014872

我只不過新增了幾個功能。

先看截圖:

  

我只說下添加和刪除按鈕的功能,其它的在上面都是上面仁兄的傑作:

添加:每點一下,會增加一個Item。

刪除:點擊會刪除選中的Item。

 

代碼如下:

CheckBoxAndListViewActivity.java

package hyz.com.checkboxandlistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import android.app.Activity;
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.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;

public class CheckBoxAndListViewActivity extends Activity implements OnClickListener,OnItemClickListener
{
	private ListView lv;
	private MyAdapter mAdapter;
	private ArrayList<String> list;
	private Button bt_cancel,bt_selectall,bt_deselectall,bt_delete,bt_add;
	private int checkNum;
	private TextView tv_show;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        bt_selectall = (Button) findViewById(R.id.bt_selectall); 
        bt_selectall.setOnClickListener(this);
        bt_cancel = (Button) findViewById(R.id.bt_cancel);
        bt_cancel.setOnClickListener(this);
        bt_deselectall = (Button) findViewById(R.id.bt_deselectall); 
        bt_deselectall.setOnClickListener(this);
        bt_delete = (Button) findViewById(R.id.bt_delete); 
        bt_delete.setOnClickListener(this);
        bt_add = (Button) findViewById(R.id.bt_add); 
        bt_add.setOnClickListener(this);        
        tv_show = (TextView) findViewById(R.id.tv);       
        list = new ArrayList<String>();
        //initDate(); 
        mAdapter = new MyAdapter(this); 
        lv = (ListView) findViewById(R.id.lv); 
        lv.setAdapter(mAdapter);
        lv.setOnItemClickListener(this); 
        dataChanged();
    }      
//    private void initDate() 
//    {       
//    	for (int i = 0; i < 15; i++)
//    	{           
//    		list.add("data" + "   " + i);   
//    	}   
//    }
    private void dataChanged()
    {           
    	mAdapter.notifyDataSetChanged(); 
    	if(list.size() != 0)
    		tv_show.setText("已選中" + checkNum + "項");
    	else
    		tv_show.setText("沒有列表項,請添加!");
    }
    @Override
	public void onClick(View v)
    {
		switch(v.getId())
		{		
		case R.id.bt_selectall:
			for(int i=0; i<list.size(); i++)
			{
				mAdapter.getIsSelected().put(i, true);
			}
			checkNum = list.size();
			dataChanged();
			break;	
		case R.id.bt_deselectall:
			for(int i=0; i<list.size(); i++)
			{
				if(mAdapter.getIsSelected().get(i))
				{
					mAdapter.getIsSelected().put(i, false);
					checkNum--;
				}
				else
				{
					mAdapter.getIsSelected().put(i, true);
					checkNum++;
				}				
			}
			dataChanged();
			break;
		case R.id.bt_cancel:
			for(int i=0;i<list.size();i++)
			{
				if(mAdapter.getIsSelected().get(i))
				{
					mAdapter.getIsSelected().put(i, false);
					checkNum--;
				}
				
			}
			dataChanged();
			break;
		case R.id.bt_add:	
			list.add("data" + "   " + list.size());
			mAdapter.getIsSelected().put(list.size()-1, false);
			dataChanged();
			break;		
		case R.id.bt_delete:			
			Iterator<Entry<Integer, Boolean>> iterator= mAdapter.getIsSelected().entrySet().iterator();		
			ArrayList<Integer> arr = new ArrayList<Integer>(); 
			while(iterator.hasNext())
			{
				Entry<Integer, Boolean>  entry = iterator.next();			    
				if(entry.getValue())
					arr.add(entry.getKey());
			}
			Integer temp;
			for(int i=0;i<arr.size()-1;i++)
				for(int j=i+1;j<arr.size();j++)
				{
					if(arr.get(i)<arr.get(j))
					{
						temp = arr.get(i);
						arr.set(i, arr.get(j));
						arr.set(j, temp);
					}
				}
			if(arr.size() != 0)
				for(int i=0;i<arr.size();i++)
					list.remove((int)arr.get(i));
			Log.i("hyz", ""+list.size());
			checkNum = 0;				
			mAdapter.getIsSelected().clear();
			mAdapter.initDate();
			dataChanged();
				
			break;		
		}    	
	}   
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,	long id) 
	{
		Log.i("hyz", "onItemClick()"+position);
		ViewHolder holder = (ViewHolder)view.getTag();
		holder.cb.toggle();
		mAdapter.getIsSelected().put(position, holder.cb.isChecked());
		if(holder.cb.isChecked() == true)
		{
			checkNum++;
		}
		else
		{
			checkNum--;
		}
		dataChanged();
	}

	class ViewHolder
	{
		TextView tv;
		CheckBox cb;
	}
	class MyAdapter extends BaseAdapter
    {
    	//private ArrayList<String> list;
    	private HashMap<Integer,Boolean> isSelected;
    	private Context context;
    	private LayoutInflater inflater = null;
    	
		public MyAdapter(Context ct)
		{
			Log.i("hyz", "MyAdapter()");
			this.context = ct;
			//this.list = list;
			inflater = LayoutInflater.from(context);
			isSelected = new HashMap<Integer,Boolean>();
			//initDate();		
		}
    	private void initDate()
    	{
    		Log.i("hyz", "initDate()"+list.size());
    		for(int i=0;i<list.size();i++)
    		{    			
    			getIsSelected().put(i,false);
    		}
    	}
		@Override
		public int getCount() 
		{	
			return list.size();
		}
		@Override
		public Object getItem(int position) 
		{
			return list.get(position);
		}
		@Override
		public long getItemId(int position) 
		{
			return position;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			ViewHolder holder = null;
			if(convertView == null)
			{
				holder = new ViewHolder();
				convertView = inflater.inflate(R.layout.list, null);
				holder.tv = (TextView) convertView.findViewById(R.id.item_tv);
				holder.cb = (CheckBox) convertView.findViewById(R.id.item_cb);
				convertView.setTag(holder);
			}
			else
			{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv.setText(list.get(position));
			Log.i("hyz", getIsSelected()+"@");
			holder.cb.setChecked(getIsSelected().get(position));
			return convertView;			
		}
		public HashMap<Integer,Boolean> getIsSelected()
		{
			return isSelected;
		}	
//		public void setIsSelected(HashMap<Integer,Boolean> isSelected)
//		{
//			this.isSelected = isSelected;
//		}    	
    }	
}


list.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">
    <TextView
        android:id="@+id/item_tv"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center_vertical"/>
	<CheckBox
	    android:id="@+id/item_cb"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"
	    android:clickable="false"
	    android:focusable="false"
	    android:focusableInTouchMode="false"
	    android:gravity="center_vertical"/>
</LinearLayout>

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">
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
		    android:id="@+id/bt_selectall"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="全選"/>
	    <Button
		    android:id="@+id/bt_deselectall"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="反選"/>		
		<Button
		    android:id="@+id/bt_cancel"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="取消"/>	    
    </LinearLayout>
    <LinearLayout 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">	   
		<Button
		    android:id="@+id/bt_add"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="添加"/> 
		<Button
		    android:id="@+id/bt_delete"
		    android:layout_width="wrap_content"
		    android:layout_height="wrap_content"
		    android:text="刪除"/>	    
    </LinearLayout>  
    <TextView
        android:id="@+id/tv"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:textSize="20dip"/>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal">
       <ListView 
        android:id="@+id/lv"
	    android:layout_width="wrap_content"
	    android:layout_height="wrap_content"/> 
    </LinearLayout>    
</LinearLayout>



 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章