Android: CheckBox OnChecked Listener for ActionMode

774 Views Asked by At

The problem: Using api's got the application I am working with complicated. The api call included processing the api's response into a listView which looks like this:

enter image description here

So for this type of layout in the listView a custom (ArrayList) adapter was required, whose code is as follows:

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;
    }
}

NEED HELP IN The checkBox now is to generate an ActionMode on check. Referring to a lot of material, I realized that one needed a custom adapter set up for that. So how can I implement two adapters ?? Or is there some other way ?? Please Help!

1

There are 1 best solutions below

2
On

Just add an onCheckChangedListener to your CheckBox by using the method vh.cb.setOnCheckedChangeListener()

You'll have to override the method onCheckedChanged(CompoundButton buttonView, boolean isChecked).

Just put in something like:

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