I am trying to declare a change listener for all checkboxes that are loaded in a listview, where the listview is also loaded by a fragment, here is my code:
the fragment:
public class AddGroupMembersFragment extends Fragment{
ArrayList<HashMap<Integer, Member>> groupMembersList;
ListView lv;
CheckBox checkBox;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_add_group_members, container, false);
groupMembersList = new ArrayList<>();
lv = (ListView) rootView.findViewById(R.id.group_members_list);
new AddGroupMembersFragment.PostDataAsyncTask().execute();
// Inflate the layout for this fragment
return rootView;
}
AdapterView.OnItemClickListener checkBoxChanged = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
// AdapterView is the parent class of ListView
ListView lv = (ListView) arg0;
if(lv.isItemChecked(position)){
if(selectAllSubmit.getVisibility()==View.INVISIBLE){
selectAllSubmit.setVisibility(View.VISIBLE);
}
}
}
};
public class PostDataAsyncTask extends AsyncTask<String, String, String> {
...
@Override
protected void onPostExecute(String lenghtOfFile) {
lv.setAdapter(new GroupMembersListAdapter(getActivity(),groupMembersList,1));
lv.setOnItemClickListener(checkBoxChanged);
}
}
the listview item.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:buttonTint="@color/colorPrimary" />
...
I want to use oncheckchangedlistener not the on itemclicklistener, any suggestions?
You need to create an Adapter and assign it to your list.
Example:
And in your fragment:
public class StackFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {
}