OnCheckedChangedListener for checkboxes that loaded in listview inside a fragment

262 views Asked by At

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?

1

There are 1 answers

0
Manza On

You need to create an Adapter and assign it to your list.

Example:

class MyAdapter extends BaseAdapter { //or ArrayAdapter, or what you prefer
    private final CompoundButton.OnCheckedChangeListener listener;
    MyAdapter(CompoundButton.OnCheckedChangeListener listener) {
        this.listener = listener;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        if (view == null) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item);
        }
        CheckBox checkBox = view.findViewById(R.id.check_box_id);
        checkBox.setOnCheckedChangeListener(listener);
        return view;

    }
}

And in your fragment:

public class StackFragment extends Fragment implements CompoundButton.OnCheckedChangeListener {

ListView listView;
MyAdapter adapter;

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    // do stuff here
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_add_group_members, container, false);
    listView = (ListView) rootView.findViewById(R.id.group_members_list);
    adapter = new MyAdapter(this);
    listView.setAdapter(adapter);
    return rootView;

}

}