Listview结合checkbox实现删除功能

这篇博客介绍了如何在Android中结合ListView和Checkbox实现删除功能。通过使用BaseAdapter类来监听ListView的动作,并采用ArrayList<HashMap<String, String>>作为数据存储结构,其中包含一个flag字段用于标记选中状态。当数据发生变化时,调用mAdapter.notifyDataSetChanged()更新视图,避免出现错误。" 112459951,10535425,C语言实现的学生信息管理系统框架,"['C语言', '文件操作', '数据结构', '链表', '程序设计']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


效果图


实现要点:

1 Listview是一个动态的组件,实现监听Listview里的动作需要用到BaseAdapter类

2 数据存储结构ArrayList<HashMap<String, String>> list, 第一个string存放flag,用以判断是否选中;第二个string存放msg

3 一旦数据有变,必须使用mAdapter.notifyDataSetChanged(); 否则view无法得知数据变化,从而会报错


一共由以下文件组成(仅核心部分,参考慎用)

JAVA

    Button btnDelete;
    ListView lv;
		Context mContext;
		MyListAdapter adapter; 
		private ArrayList<HashMap<String, String>> list;
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        
        requestWindowFeature(Window.FEATURE_NO_TITLE); 
        setContentView(R.layout.activity_carton_no_main);
        SysApplication.getInstance().addActivity(this); 
        
        btnDelete = (Button)findViewById(R.id.ctn_no_scan_delete_btn); 
        lv = (ListView)findViewById(R.id.carton_no_list);
        
				list = new ArrayList<HashMap<String, String>>();  
				mAdapter = new MyListAdapter(list, this);
				lv.setAdapter(mAdapter);
				
				//insert something
				HashMap<String, String> map = new HashMap<String, String>();
  			map.put("content", 'test');
  			map.put("flag", "false");
  			list.add(map); 
  			
  			btnDelete.setOnClickListener(new View.OnClickListener() { 
					@Override
					public void onClick(View v) { 
						Iterator<HashMap<String, String>> iterator = list.iterator();
						while (iterator.hasNext()) {
							HashMap<String, String> temp = iterator.next();
							if (temp.get("flag").equals("true")) {
								iterator.remove();
							}
						}
						checkNum = 0;
						//if you have changed list, please excute mAdapter.notifyDataSetChanged(); 
						dataChanged();
					}
				});
  			
  			lv.setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
							long arg3) {  
						ViewHolder holder = (ViewHolder) arg1.getTag();  
						holder.cb.toggle();
						
						//store checkbox's status 
						if (holder.cb.isChecked() == true) {
							list.get(arg2).put("flag", "true");
							checkNum++;
						} else {
							list.get(arg2).put("flag", "false");
							checkNum--;
						} 
					}
				});
    }
		public class MyListAdapter extends BaseAdapter{
		
				private ArrayList<HashMap<String, String>> list;
				 
				private Context context;
				 
				private LayoutInflater inflater = null;
				 
				public MyListAdapter(ArrayList<HashMap<String, String>> list, Context context){
					this.context = context;
					this.list = list;
					inflater = LayoutInflater.from(context);
				}
				
				@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.activity_carton_no_list, null);
						holder.tv = (TextView) convertView.findViewById(R.id.carton_no_list_name);
						holder.cb = (CheckBox) convertView.findViewById(R.id.carton_no_list_checked); 
						convertView.setTag(holder);
					} else { 
						holder = (ViewHolder) convertView.getTag();
					}
					//init view
					holder.tv.setText(list.get(position).get("content").toString()); 
					holder.cb.setChecked(list.get(position).get("flag").equals("true"));
					return convertView;
				}
				
				final class ViewHolder{
					TextView tv;
					CheckBox cb; 
				}
			}
		private void dataChanged(){
		
	  mAdapter.notifyDataSetChanged(); 
	  }


XML 

1/2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:orientation="vertical" >
    
  <LinearLayout
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:descendantFocusability="blocksDescendants"> 
	  
      <CheckBox
        android:id="@+id/carton_no_list_checked"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="false"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:gravity="center_vertical" />

	  <TextView
	      android:id="@+id/carton_no_list_name"
	      android:layout_width="fill_parent"
	      android:layout_height="wrap_content"
	      android:layout_gravity="center"
	      android:layout_marginLeft="10dp"
	      android:layout_weight="1"
	      android:text="Name"
	      android:textColor="@color/viewfinder_mask"
	      android:textSize="20dp" />
	   
  </LinearLayout>
</LinearLayout>

2/2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:orientation="vertical" >
 
	<LinearLayout  
	    android:orientation="vertical"
	    android:layout_width="match_parent"
	    android:layout_height="match_parent" 
	    android:layout_weight="0.1"> 
	    <ListView
	       android:id="@+id/carton_no_list"
	       android:layout_width="match_parent"
	       android:layout_height="fill_parent"/>
	</LinearLayout>
 	 	<LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"   
        android:layout_weight="0.7"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/ctn_no_scan_delete_btn"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_weight="0.1" 
            android:text="Delete" /> 
         
    </LinearLayout>  
</LinearLayout>


同理,实现radiobox,也是一样的道理。


One thing leads to another...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值