gpt4 book ai didi

Android recyclerview 在数据更改后显示更多值

转载 作者:行者123 更新时间:2023-12-05 00:18:08 24 4
gpt4 key购买 nike

我正在尝试编辑 中的项目回收站查看 它显示了来自 Firebase 的一些数据。但编辑后的数据集并没有改变。它显示 上一个 以及新的更新 项目,直到 Activity 为 重装 .截图在最后给出。
我正在通过 编辑项目警报对话框 .以下是代码:

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.MyViewHolder> {
ArrayList<User_Post> user_posts=null;
Context context;
public ListAdapter(@Nullable ArrayList<User_Post> posts, Context c) {
this.user_posts=posts; // the data set
this.context=c;
}
根据项目的 编辑 按钮点击 AlertDialog打开:
  //'post' is an instance of type User_Post containing the current data item.

dialogBuilder.setPositiveButton("Save", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if(isValid()) {
post.setWorkType(txtType.getSelectedItem().toString());
post.setWorkDescription(txtDetails.getText().toString());

user_posts.set(position,post);
updateDataSet(user_posts);

saveToFirebase(post,"edit");
Toast.makeText(context, "Post updated", Toast.LENGTH_SHORT).show();

}
}
}
更新 firebase 数据的代码:
private void saveToFirebase(User_Post post, String task) {
String id=post.getPostID();

FirebaseDatabase database = FirebaseDatabase.getInstance();
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://smart-worker-c73c4.appspot.com/gs:");
DatabaseReference myRef = database.getReference("posts");

if(task.equals("edit")) {
myRef.child(id).setValue(post);
}
else {
myRef.child(id).setValue(null);
storageRef.child(id).delete();
}
}
对于更新数据集:
private void updateDataSet(ArrayList<User_Post> posts){
this.user_posts=posts;
notifyDataSetChanged();
}
编辑前的截图...
enter image description here
修改后的截图...
enter image description here

最佳答案

It shows previous as well as the new updated items, until the activity is reloaded


因此,当您重新加载 Activity 时,新数据会显示在 RecyclerView 上。 ,因此数据成功保存到firebase中,并在重新加载 Activity 时被抓取。所以,这个问题与firebase无关。
此外,您决定不对 firebase 使用持久事件监听器,而是更新了 RecyclerView本地有:
 private void updateDataSet(ArrayList<User_Post> posts){
this.user_posts = posts;
notifyDataSetChanged();
}
虽然我建议清除,然后在通知更改之前重建列表:
 private void updateDataSet(ArrayList<User_Post> posts){
this.user_posts.clear();
this.user_posts.addAll(posts);
notifyDataSetChanged();
}
这不是最佳程序,因为调用 notifyDataSetChanged()太贵了;相反,我建议 notifyItemInserted () notifyItemRangeChanged() ;仅部署对 RecyclerView 的更改.
例如,如果您只传递一个项目:
 private void updateDataSet(User_Post post){
this.user_posts.add(post);
notifyItemInserted(this.user_posts.size()-1);
}
对于一系列项目:
 private void updateDataSet(ArrayList<User_Post> posts){ // Pass only those items that are changed
int currentSize = this.user_posts.size();
this.user_posts.addAll(posts);
int newSize = this.user_posts.size();
notifyItemInserted(currentSize, newSize - 1);
}

关于Android recyclerview 在数据更改后显示更多值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69425872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com