简体   繁体   中英

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:

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 ; I want now to update the item parameter text_wall_post of the recyclerView ?

How could I update a recyclerView item specific parameter ?

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.

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)

    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

Then get the item with specific position from postList and update the parameter you want

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

And finally call

postAdapter.notifyItemChanged (position)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM