Get Selected Item Using Checkbox in Listview

前端 未结 9 702
醉酒成梦
醉酒成梦 2020-11-22 09:36

I am creating an Android application where I have a ListView that displays all of the applications that were installed in my mobile phone.

My ListView is customized,

9条回答
  •  盖世英雄少女心
    2020-11-22 09:53

    It's a simplifications but very easy... You need to add the the focusable flag to the checkbox, as written before. You need also to add the clickable flag, as shown here:

    android:focusable="false"
    android:clickable="false"
    

    Than you control the checkbox state from within the ListView (ListFragment in my case) onListItemClick event.

    This the sample onListItemClick method:

    public void onListItemClick(ListView l, View v, int position, long id) {
    super.onListItemClick(l, v, position, id);
    //Get related checkbox and change flag status..
    CheckBox cb = (CheckBox)v.findViewById(R.id.rowDone);
    cb.setChecked(!cb.isChecked());
    Toast.makeText(getActivity(), "Click item", Toast.LENGTH_SHORT).show();
    }
    

提交回复
热议问题