是否可以通过RecyclerView.Adapter类更新某些列表数据
这是我的List类:
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;
}
}
我的RecyclerView适配器类:
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);
}
}
}
当用户单击 checkBoxLikeIdeaListFragment 时,我想更新我的idea.getVoted() 我不太确定我将如何在此部分使用 ideaList.set() 。
我应该在我的 MainActivity 上执行此操作吗?如果是,请教我。
答案 0 :(得分:2)
在模型类**Idea**
上创建Setter方法,并访问setter方法以更新数据。
喜欢这个。
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;
}
}
在适配器类中使用setVoted
的setter方法。
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();
}
});
答案 1 :(得分:1)
最快的方法是在OnCheckedListener
构造函数上传递IdeaViewHolder
并将其分配给checkBoxLikeIdeaListFragment
。
或者在您现有的代码上,您可以:
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
}
});