Android:ActionBox的CheckBox OnChecked Listener

时间:2013-08-25 15:38:20

标签: android api listview android-actionmode

问题: 使用api得到了我正在处理复杂的应用程序。 api调用包括将api的响应处理成listView,如下所示:

enter image description here

因此,对于listView中的这种类型的布局,需要一个自定义(ArrayList)适配器,其代码如下:

public class ArrayListAdapter extends BaseAdapter{

    public Context mContext;
    public LayoutInflater mInflater;
    public ArrayList<HashMap<String,String>> mData;
    private SparseBooleanArray mSelectedItemsIds;


    public ArrayListAdapter(Context context, ArrayList<HashMap<String,String>> data){
        mSelectedItemsIds = new SparseBooleanArray();
        mData = data;
        this.mContext = context;
        mInflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mData.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        ViewHolder vh;

        if(convertView == null){
            vh = new ViewHolder();
            convertView = mInflater.inflate(R.layout.projectlist_frame, null);
            vh.projectTitle = (TextView)convertView.findViewById(R.id.projecttitle);
            vh.projectSector = (TextView)convertView.findViewById(R.id.projectsector);
            vh.cb = (CheckBox)convertView.findViewById(R.id.checkBox1);
            convertView.setTag(vh);

        } else{
            vh = (ViewHolder)convertView.getTag();
        }

        vh.projectTitle.setText(mData.get(position).get("title").toString());
        vh.projectSector.setText(mData.get(position).get("sector").toString());

        return convertView;
    }

    class ViewHolder{
        TextView projectTitle, projectSector;
        CheckBox cb;
    }
}

需要帮助 checkBox现在是在检查时生成ActionMode。参考了很多材料,我意识到需要为此设置一个自定义适配器。那么如何实现两个适配器?还是有其他方式?请帮助!

1 个答案:

答案 0 :(得分:1)

只需使用方法vh.cb.setOnCheckedChangeListener()

onCheckChangedListener添加到CheckBox即可

你必须覆盖onCheckedChanged方法(CompoundButton buttonView,boolean isChecked)。

只需输入:

@override
private void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if(isChecked){
//box is checked
}else{
//box is unchecked
}