Realm-java: Change checkbox on RecyclerView without save selected items into database

Created on 19 Oct 2016  ·  6Comments  ·  Source: realm/realm-java

How can i use checkbox on RecyclerView without save selected items into database? when i use model which is extends from RealmObject on recyclerview, for every change one of this model must be we use Transaction, :| but i dont want to save it to database, its only for getting user selected Items, i can't resolve this problem

T-Help

Most helpful comment

How about this?

public class Model extends RealmObject {
    @PrimaryKey
    public String id;
    public String text;
}

public class MyAdapter extends RealmRecyclerViewAdapter<Model, MyAdapter.Holder> {
    private Map<String, Boolean> checkBoxStates = new HashMap<>();

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final Model item = getItem(position);
        holder.text.setText(item.text);

        Boolean checkedState = checkBoxStates.get(item.id);
        holder.checkbox.setChecked(checkedState == null ? false : checkedState);
    }

    // call this when the checked state is changed
    private void onCheckChanged(int position, boolean checked) {
        final Model item = getItem(position);
        if (item == null) {
            return;
        }
        checkBoxStates.put(item.id, checked);
    }

    static class Holder extends RecyclerView.ViewHolder {
        public final TextView text;
        public final CheckBox checkbox;

        Holder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(android.R.id.text1);
            checkbox = (CheckBox) itemView.findViewById(android.R.id.checkbox);
            // some more code...
        }
    }
}

All 6 comments

I think this needs #3466 for easy implementation.

I think you need to have something like extra boolean array in the Adapter to keep the states of the checkboxes for now.

checkbox are model items, this solution is very hard to manage that, because of maybe i have to create other class like with current model

How about this?

public class Model extends RealmObject {
    @PrimaryKey
    public String id;
    public String text;
}

public class MyAdapter extends RealmRecyclerViewAdapter<Model, MyAdapter.Holder> {
    private Map<String, Boolean> checkBoxStates = new HashMap<>();

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        final Model item = getItem(position);
        holder.text.setText(item.text);

        Boolean checkedState = checkBoxStates.get(item.id);
        holder.checkbox.setChecked(checkedState == null ? false : checkedState);
    }

    // call this when the checked state is changed
    private void onCheckChanged(int position, boolean checked) {
        final Model item = getItem(position);
        if (item == null) {
            return;
        }
        checkBoxStates.put(item.id, checked);
    }

    static class Holder extends RecyclerView.ViewHolder {
        public final TextView text;
        public final CheckBox checkbox;

        Holder(View itemView) {
            super(itemView);
            text = (TextView) itemView.findViewById(android.R.id.text1);
            checkbox = (CheckBox) itemView.findViewById(android.R.id.checkbox);
            // some more code...
        }
    }
}

@zaki50 Thanks i think by this solution my problem will be resolved

😁

Was this page helpful?
0 / 5 - 0 ratings