java - 通知 RecyclerView 中 Firestore 数据的更改

标签 java android android-recyclerview google-cloud-firestore notifydatasetchanged

在我的应用程序中,有一个 fragment ,其中包含可扩展的 RecyclerView 从 firebase firestore 获取数据,其中的每个项目都有一个按钮,单击该按钮会更改 Firestore 中的某些数据,从而以某种方式影响项目。 现在的问题是:当运行应用程序并单击按钮时,数据不会更改,直到我刷新 fragment ..

fragment.java:

View root = inflater.inflate(R.layout.fragment_dashboard, container, false);
final String UserId = currentUser.getUid();

final RecyclerView recyclerView = root.findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

db.collectionGroup("Id").whereEqualTo("id", UserId).get()
  .addOnCompleteListener(new OnCompleteListener < QuerySnapshot > () {
    @Override
    public void onComplete(@NonNull Task < QuerySnapshot > task) {
        final List < Company > Parent = new ArrayList < > ();

        for (QueryDocumentSnapshot document: task.getResult()) {
            final List < String > grades = (List < String > ) document.get("tagsG");

            CollectionReference collectionReference =
                document.getReference().getParent();
            final List < Product > Child = new ArrayList < > ();
            for (int i = 0; i < grades.size(); i++) {
                final String gg = grades.get(i);
                collectionReference.document(UserId).collection(gg).whereEqualTo("approvedStd",0).get()
                    .addOnCompleteListener(new OnCompleteListener < QuerySnapshot > () {
                        @Override
                        public void onComplete(@NonNull Task < QuerySnapshot > task) {
                            for (QueryDocumentSnapshot document: task.getResult()) {
                                String name = document.getString("std_name");
                                String sub = document.getString("std_subject");
                                String region = document.getString("std_region");
                                Child.add(new Product(name, sub, region, gg));
                            }
                        }
                    });
            }
            Parent.add(new Company("new", Child));
            productAdapter = new ProductAdapter(Parent);
            recyclerView.setAdapter(productAdapter);
        }
    }
});
return root;

ViewHolder.java

public class ProductViewHolder extends ChildViewHolder {
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private FirebaseAuth mAuth = FirebaseAuth.getInstance();
    private FirebaseUser currentUser = mAuth.getCurrentUser();
    private String UserId = currentUser.getUid();
    private TextView name;
    private TextView sTextView;
    private TextView region;
    private Button addButton;
    private String phoneOfstd;
    private String grade;
    private ProductAdapter productAdapter;

    public ProductViewHolder(View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        sTextView = itemView.findViewById(R.id.sub);
        region = itemView.findViewById(R.id.region);
    }

    public void bind(Product product) {
        name.setText(product.name);
        sTextView.setText(product.sub);
        region.setText(product.region);
        grade = product.grade;

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                CollectionReference questionsRef = db.collection("Teachers checked").document("courses").collection("Id");
                DocumentReference docRef = questionsRef.document(UserId).collection(grade).document(phoneOfstd);
                docRef.update("approvedStd", 1);
                productAdapter.notifyDataSetChanged();
            }
        });
    }

}

产品适配器.java

public class ProductAdapter extends
 ExpandableRecyclerViewAdapter < CompanyViewHolder, ProductViewHolder > {
    public ProductAdapter(List < ? extends ExpandableGroup > groups) {
        super(groups);
    }

    @Override
    public CompanyViewHolder onCreateGroupViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_recyclerview_company, parent, false);
        return new CompanyViewHolder(v);
    }

    @Override
    public ProductViewHolder onCreateChildViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_recyclerview_product, parent, false);
        return new ProductViewHolder(v);
    }

    @Override
    public void onBindChildViewHolder(ProductViewHolder holder, int flatPosition, ExpandableGroup group, int childIndex) {
        final Product product = (Product) group.getItems().get(childIndex);
        holder.bind(product);
    }

    @Override
    public void onBindGroupViewHolder(CompanyViewHolder holder, int flatPosition, ExpandableGroup group) {
        final Company company = (Company) group;
        holder.bind(company);
    }
}

编辑:

我尝试在 recyclerView.setAdapter(productAdapter); 之后的 fragment 中写入 productAdapter.notifyDataSetChanged(); 但应用程序崩溃并且 logcat 显示此错误:

FATAL EXCEPTION: main java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.exercise.ExpandableRecyclerView.ProductAdapter. notifyDataSetChanged()' on a null object reference

最佳答案

这里可能是你的错误docRef.update("approvedStd", 1); ProductAdapter.notifyDataSetChanged();

您更新了firestore中的对象,但没有实现EventListener来在发生任何更改后检索数据,因此您有两个选项一来更新arrayList中的此对象并调用notifydatasetChanged或添加Snapshotlistener以在执行任何更改后检索数据

关于java - 通知 RecyclerView 中 Firestore 数据的更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60806398/

相关文章:

java - map 。 java 。返回值

Java 在带有 Switch Case 的 While 循环中连续绘制图像

java - 在计时器任务中获取 NPE

android - 如何识别我的应用程序是否被用户设置为默认?

android - 从 RecyclerView 单击 CardView 加载 fragment

java - 选项卡布局内的水平回收器 View

java - 回收者 View 不断刷新

java - 如何从 wsdl 生成请求和响应类?

java - 映射到 Charsequence[] 但不按顺序

android - Firebase 通知 - 无效的注册 token ,请检查 Android 中的 token 格式