totalnoob
totalnoob

Reputation: 33

Update certain item in List data from RecyclerView.Adapter class

Is there any way to update certain List data from RecyclerView.Adapter class

here is my List class:

public class Idea {
    private String id, name, voted;

    public Idea(String id, String name, String voted) {
        this.id = id;
        this.name = name;
        this.voted = voted;
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public String getVoted() {
        return voted;
    }
}

my RecyclerView Adapter class:

public class IdeaListAdapter extends RecyclerView.Adapter<IdeaListAdapter.IdeaViewHolder> {

    private Context mCtx;
    private List<Idea> ideaList, idlst;

    public IdeaListAdapter(Context mCtx, List<Idea> ideaList) {
        this.mCtx = mCtx;
        this.ideaList = ideaList;
    }

    @NonNull
    @Override
    public IdeaListAdapter.IdeaViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater inflater = LayoutInflater.from(mCtx);
        View view = inflater.inflate(R.layout.idea_list_object_layout, null);
        return new IdeaListAdapter.IdeaViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull IdeaListAdapter.IdeaViewHolder ideaViewHolder, int i) {
        final Idea idea = ideaList.get(i);

        if (idea.getVoted().equals("0")) {
            ideaViewHolder.checkBoxLikeIdeaListFragment.setChecked(false);
        } else if (idea.getVoted().equals("1")) {
            ideaViewHolder.checkBoxLikeIdeaListFragment.setChecked(true);
        }

        ideaViewHolder.checkBoxLikeIdeaListFragment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    *** Update my idea.getVoted() here ***
                } else {
                    *** Update my idea.getVoted() here ***
                }
            }
        });
        ideaViewHolder.textViewNameIdeaListFragment.setText(idea.getName());

    }

    @Override
    public int getItemCount() {
        return ideaList.size();
    }

    class IdeaViewHolder extends RecyclerView.ViewHolder {

        TextView textViewNameIdeaListFragment;
        CheckBox checkBoxLikeIdeaListFragment;

        public IdeaViewHolder(@NonNull View itemView) {
            super(itemView);

            textViewNameIdeaListFragment = (TextView) itemView.findViewById(R.id.textViewNameIdeaListFragment);
            checkBoxLikeIdeaListFragment = (CheckBox) itemView.findViewById(R.id.checkBoxLikeIdeaListFragment);
        }
    }

}

i would like to update my idea.getVoted() when the user click the checkBoxLikeIdeaListFragment im not quite sure how i will use the ideaList.set() at this part.

should i do it on my MainActivity ? if yes, please teach me.

Upvotes: 0

Views: 495

Answers (2)

Kaushal Panchal
Kaushal Panchal

Reputation: 1825

Create Setter method on your Model class **Idea** and access setter method to update the data.

Like this.

public class Idea {
    private String id, name, voted;

    public Idea(String id, String name, String voted) {
        this.id = id;
        this.name = name;
        this.voted = voted;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getVoted() {
        return voted;
    }

    public void setVoted(String voted) {
        this.voted = voted;
    }
}

Use setter method of setVoted in your adapter class.

ideaViewHolder.checkBoxLikeIdeaListFragment.setOnCheckedChangeListener(
            new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            i
            ideaList.get(i).setVoted("set your data"); //here you have to upadte the vote value.
            notifiyDatasetChanged(); 
        }
    });

Upvotes: 2

MetaSnarf
MetaSnarf

Reputation: 6187

Quickest way to do this is to pass a OnCheckedListener on your IdeaViewHolder constructor and assign it to your checkBoxLikeIdeaListFragment.

Or on your existing code you can:

ideaViewHolder.checkBoxLikeIdeaListFragment.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                   ideaList.get(i).setVoted(b); // i is the int index passed to onBindViewHolder(..)
                  IdeaListAdapter.this.notifyDatasetChanged(); //update list 


            }
        });

Upvotes: 1

Related Questions