[英]How to update complex recyclerview item parameters?
here is my post item:这是我的帖子:
public class PostItems {
private String id_wall_post, id_user, text_wall_post, ..........;
public PostItems(String id_wall_post, String id_user, String text_wall_post, ..............) {
this.id_wall_post = id_wall_post;
this.id_user = id_user;
this.text_wall_post = text_wall_post;
}
String get_id_wall_post() {
return id_wall_post;
}
String get_id_user() {
return id_user;
}
String get_text_wall_post() {
return text_wall_post;
}
...
...
.........
...
...
}
Here my adapter:这是我的适配器:
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
private List<PostItems> postList;
private Context context;
PostAdapter(List<PostItems> postList, Context context) {
this.postList = postList;
this.context = context;
}
@NonNull
@Override
public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_view_wall_post, parent, false);
return new PostViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
PostItems post_items = postList.get(position);
...
...
.........
...
...
}
@Override
public int getItemCount() {
return postList.size();
}
static class PostViewHolder extends RecyclerView.ViewHolder {
PostViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
Using retrofit the recyclerview is populated correctly:使用改造正确填充了 recyclerview:
postList.addAll(response.body());
postAdapter = new PostAdapter(postList, ProfileActivity.this);
recyclerView.setAdapter(postAdapter);
I also have an Editext where user can update the value of text_wall_post ;我还有一个 Edittext,用户可以在其中更新text_wall_post的值; I want now to update the item parameter text_wall_post of the recyclerView ?我现在想更新 recyclerView 的项目参数text_wall_post吗?
How could I update a recyclerView item specific parameter ?如何更新 recyclerView 项目特定参数?
EDIT: I have found that:编辑:我发现:
postList.set(1, new PostItems("1","1176", "some text update here", ..................));
postAdapter.notifyItemChanged(1);
In my recyclerView, I have more than 20 parameters.在我的 recyclerView 中,我有 20 多个参数。
Which work but I want a more clean way to update only the needed parameter without updating all the parameters.哪个有效,但我想要一种更干净的方法来仅更新所需的参数而不更新所有参数。
Thanks for Any help.谢谢你的帮助。
You can change an item without creating a new one.您可以更改项目而无需创建新项目。 First make setters (like the getters you made) to change a specific parameter from an object( postItems
in this case)首先创建 setter(如您创建的 getter)以更改对象的特定参数(在本例中为postItems
)
public class PostItems {
private String id_wall_post, id_user, text_wall_post, ..........;
public PostItems(String id_wall_post, String id_user, String text_wall_post, ..............) {
this.id_wall_post = id_wall_post;
this.id_user = id_user;
this.text_wall_post = text_wall_post;
}
String get_id_wall_post() {
return id_wall_post;
}
String get_id_user() {
return id_user;
}
String get_text_wall_post() {
return text_wall_post;
}
public void setIdWallPost(String idWallpost){
this.id_wall_post = idWallpost;
}
public void setIdUser(String idUser){
this.id_user = idWallUser;
}
public void setTextWallPost(String textWallPost){
this.text_wall_post = textWallPost;
}
...
...
.........
...
...
}
You can also make android studio generate them for you by pressing Alt+Inser
> Setter
您还可以通过按Alt+Inser
> Setter
让 android studio 为您生成它们
Then get the item with specific position from postList
and update the parameter you want然后从postList
获取具有特定位置的项目并更新您想要的参数
postList.get(position).setTextWallPost("new value here");
And finally call最后打电话
postAdapter.notifyItemChanged (position)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.