XML : Android checkbox with custom selector inside recyclerview

I made recyclerview with checkbox so you can select each item and button on bottom of the screen when pressed return selected items from list.The problem is that no matter what i do button always return that 0 items were selected.I found that checkbox status is always false even thought icon from checkbox change when i click checkbox. I use custom selector to change icon inside checkbox but i can't find the way to change checkbox status inside program.

This is the fragment that contain recyclerview

      @Override  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {      View rootView = inflater.inflate(R.layout.fragment_main, container, false);      Resources res = getResources();      titles = res.getStringArray(R.array.muscle_titles);        recyclerMain = (RecyclerView) rootView.findViewById(R.id.MainList);      adapter = new MainAdapter(getActivity(), getData(getActivity()));      recyclerMain.setAdapter(adapter);      recyclerMain.setLayoutManager(new LinearLayoutManager(getActivity()));      return rootView;  }    public static List<MainRowInformation> getData(Context context){      List<MainRowInformation> data = new ArrayList<>();          for(int i=0;i<titles.length;i++){            final MainRowInformation current = new MainRowInformation();          current.title = titles[i];          current.checkBox = new CheckBox(context);          current.checkBox.setChecked(false);              data.add(current);      }        return data;  }    

This is recyclerview adapter

      private LayoutInflater inflater;  private List<MainRowInformation> data = Collections.emptyList();    public MainAdapter(Context context, List<MainRowInformation> data) {      this.data = data;      inflater = LayoutInflater.from(context);  }    @Override  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {      View view = inflater.inflate(R.layout.row_main, parent, false);      MyViewHolder holder = new MyViewHolder(view);      return holder;  }    @Override  public void onBindViewHolder(final MyViewHolder holder, int position) {      MainRowInformation current = data.get(position);      holder.title.setText(current.title);      //mozda je krivo sljedeca linija      holder.checkbox = current.checkBox;    }    @Override  public int getItemCount() {      return data.size();  }    class MyViewHolder extends RecyclerView.ViewHolder{        TextView title;      CheckBox checkbox;        public MyViewHolder(View itemView) {          super(itemView);          title = (TextView) itemView.findViewById(R.id.MainListText);          checkbox = (CheckBox) itemView.findViewById(R.id.MainListCheckBox);        }  }    

Checkbox

      <CheckBox      android:id="@+id/MainListCheckBox"      android:layout_width="wrap_content"      android:layout_height="wrap_content"      android:button="@drawable/main_list_button_icon"/>    

Checkbox Selector

  <selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:drawable="@mipmap/ic_list_uncheck"      android:state_checked="false" />  <item android:drawable="@mipmap/ic_list_check"      android:state_checked="true"/>    

the button return 0 but 3 items were selected

No comments:

Post a Comment