欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  技术分享

ListView checkbox_Android之记录ListView的checkbox选择位置

程序员文章站 2022-03-01 17:44:50
...

多的不说、上代码

<CheckBox 
        android:id="@+id/checkbox"
        android:focusable="false" // 必须有
        android:clickable="false"  // 必须有
        android:layout_marginLeft="20dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
public class ListViewCheckBoxActivity extends Activity {
    private ListView listview;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        
        listview = (ListView) findViewById(R.id.listview);
        MyAdapter adapter=new MyAdapter(this);    
        listview.setAdapter(adapter);    
        listview.setItemsCanFocus(false);    
        listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);    
        listview.setOnItemClickListener(new OnItemClickListener(){    
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                System.out.println("aa");
                ViewHolder vHollder = (ViewHolder) view.getTag();    
                //在每次获取点击的item时将对于的checkbox状态改变,同时修改map的值。    
                vHollder.cBox.setChecked(true);
                boolean check = vHollder.cBox.isChecked();
                MyAdapter.isSelected.put(position, check); 
            }    
        });  
    }
}
package com.lee0000.ListViewCheckBox;
import java.util.ArrayList;    
import java.util.HashMap;    
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.BaseAdapter;    
import android.widget.CheckBox;    
import android.widget.ImageView;    
import android.widget.TextView;    
    
public class MyAdapter extends BaseAdapter {    
    private LayoutInflater mInflater;    
    private List<Map<String, Object>> mData;    
    public static Map<Integer, Boolean> isSelected;    
    public MyAdapter(Context context) {    
        mInflater = LayoutInflater.from(context);    
        init();    
    }    
    
    //初始化    
    private void init() {    
        mData=new ArrayList<Map<String, Object>>();    
        for (int i = 0; i < 25; i++) {    
            Map<String, Object> map = new HashMap<String, Object>();    
            map.put("title", "第" + (i + 1) + "行的标题");    
            mData.add(map);    
        }    
        
        //这儿定义isSelected这个map是记录每个listitem的状态,初始状态全部为false。    
        isSelected = new HashMap<Integer, Boolean>();    
        for (int i = 0; i < mData.size(); i++) {    
            isSelected.put(i, false);    
        }    
    }    
    
    @Override    
    public int getCount() {    
        return mData.size();    
    }    
    
    @Override    
    public Object getItem(int position) {    
        return mData.get(position);    
    }    
    
    @Override    
    public long getItemId(int position) {    
        return position;    
    }    
    
    @Override    
    public View getView(int position, View convertView, ViewGroup parent) {    
        ViewHolder holder = null;    
        //convertView为null的时候初始化convertView。    
        if (convertView == null) {    
            holder = new ViewHolder();    
            convertView = mInflater.inflate(R.layout.listviewcontent, null);    
            holder.title = (TextView) convertView.findViewById(R.id.text);    
            holder.cBox = (CheckBox) convertView.findViewById(R.id.checkbox);    
            convertView.setTag(holder);    
        } else {    
            holder = (ViewHolder) convertView.getTag();    
        }    
         
        holder.title.setText(mData.get(position).get("title").toString());    
        holder.cBox.setChecked(isSelected.get(position));    
        return convertView;    
    }    
    
    public class ViewHolder {    
        public TextView title;    
        public CheckBox cBox;    
    }    
}