英文:
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);
}
}
}
使用 Retrofit,RecyclerView 正确地被填充:
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<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.
答案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` > `Setter` 来让Android Studio为您生成setter方法。
然后,通过特定位置从`postList`获取项目,并更新您想要的参数<br />
```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("new value here");
And finally call<br/>
postAdapter.notifyItemChanged (position)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论