如何更新复杂的RecyclerView项参数?

huangapple go评论87阅读模式
英文:

How to update complex recyclerview item parameters?

问题

以下是翻译好的内容:

这是我的帖子项目

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

使用 RetrofitRecyclerView 正确地被填充

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

我还有一个 EditText用户可以更新 **text_wall_post** 的值我现在想要更新 recyclerView 中的项目参数 **text_wall_post** 

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

**编辑**
我发现

postList.set(1, new PostItems("1","1176", "一些更新的文本在这里", ..................));
postAdapter.notifyItemChanged(1);


**在我的 recyclerView 中,我有超过 20 个参数。**
这个方法可以工作,但我希望有一种更干净的方法只更新所需的参数,而不是更新所有参数。
感谢任何帮助。
英文:

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&lt;PostAdapter.PostViewHolder&gt; {
private List&lt;PostItems&gt; postList;
private Context context;
PostAdapter(List&lt;PostItems&gt; 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(&quot;1&quot;,&quot;1176&quot;, &quot;some text update here&quot;, ..................));
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.

答案1

得分: 1

可以在不创建新项的情况下更改项目。
首先创建setter方法(类似于您创建的getter方法),用于更改对象中的特定参数(在这种情况下为postItems)<br />

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 = idUser;
    }
    public void setTextWallPost(String textWallPost){
        this.text_wall_post = textWallPost;
    }
    ...
    ...
    .........   
    ...
    ...
}

您还可以通过按下`Alt+Insert` &gt; `Setter` 来让Android Studio为您生成setter方法

然后通过特定位置从`postList`获取项目并更新您想要的参数&lt;br /&gt;

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

最后调用<br/>

postAdapter.notifyItemChanged(position);
英文:

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)<br />

    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<br />

postList.get(position).setTextWallPost(&quot;new value here&quot;);

And finally call<br/>

postAdapter.notifyItemChanged (position)

huangapple
  • 本文由 发表于 2020年10月2日 17:46:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/64169496.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定