如何更新复杂的recyclerview项目参数?

时间:2020-10-02 09:46:31

标签: java android-recyclerview

这是我的帖子:

    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 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);
        }
    }
}

使用翻新版,可正确填充recyclerview:

postList.addAll(response.body());
  postAdapter = new PostAdapter(postList, ProfileActivity.this);
  recyclerView.setAdapter(postAdapter);

我还有一个Editext,用户可以在其中更新 text_wall_post 的值;我现在要更新recyclerView的项目参数 text_wall_post 吗?

如何更新recyclerView项目的特定参数?

编辑: 我发现:

postList.set(1, new PostItems("1","1176", "some text update here", ..................));
postAdapter.notifyItemChanged(1);

在我的recyclerView中,我有20多个参数。

这是什么工作,但我希望有一种更干净的方法来仅更新所需的参数,而不更新所有参数。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您可以更改一项而无需创建新项。 首先进行设置器(如您创建的获取器)来更改对象的特定参数(在这种情况下为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;
}
...
...
.........
...
...
}

您还可以通过按Alt+Inser> Setter

使android studio为您生成它们

然后从postList获取具有特定位置的项目,并更新所需的参数

postList.get(position).setTextWallPost("new value here");

最后打电话给

postAdapter.notifyItemChanged (position)