如何将所有文档放入查询Firestore Android

时间:2020-04-18 12:47:44

标签: android google-cloud-firestore

如何获取所有文档并将其放入查询中?我收到的错误是java.lang.NullPointerException:提供的文档路径不能为null。我还添加了数据库结构。我的数据库中有一个集合,而该集合中有另一个子集合,我想显示我所有集合中的子集合数据。

enter image description here

管理片段

public class AdminFragment extends Fragment implements FirestoreAdapter.OnItemClickListener {
    RecyclerView myRecycleView;
    private FirestoreAdapter adapter;
    FirebaseFirestore fStore;
    private String email = "";
    Button Btn;
    Button Btn2;
    FirebaseAuth fAuth;
    String uid;
    private String userId ="";
    private Integer position = 0;
    FirebaseUser firebaseUser;

    @SuppressLint("ResourceType")
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_admin, container, false);
        myRecycleView = v.findViewById(R.id.recyclerView);
        Btn = v.findViewById(R.id.editBtn);
        Btn2 = v.findViewById(R.id.deleteBtn);
        fAuth = FirebaseAuth.getInstance();
        fStore = FirebaseFirestore.getInstance();

        uid = fAuth.getInstance().getUid();


 /*error*/       Query query = fStore.collection("users").document(uid).collection("Detail");
        PagedList.Config config = new PagedList.Config.Builder().setInitialLoadSizeHint(10).setPageSize(3).build();
        FirestorePagingOptions<UserInfo> options = new FirestorePagingOptions.Builder<UserInfo>().setLifecycleOwner(this)
                .setQuery(query, config, new SnapshotParser<UserInfo>() {
                    @NonNull
                    @Override
                    public UserInfo parseSnapshot(@NonNull DocumentSnapshot snapshot) {
                        UserInfo userInfo = snapshot.toObject(UserInfo.class);
                        String itemId = snapshot.getId();
                        userInfo.setItem_id(itemId);
                        return userInfo;
                    }
                }).build();

        adapter = new FirestoreAdapter(options, this);

        myRecycleView.setHasFixedSize(true);
        myRecycleView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        myRecycleView.setAdapter(adapter);

        Btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fAuth.sendPasswordResetEmail(email)

                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {

                                if (task.isSuccessful()) {
                                    Toast.makeText(getActivity(), "Password Reset link sent to your registered E-Mail!", Toast.LENGTH_LONG).show();

                                }else{
                                    Toast.makeText(getActivity(), "Failed to send reset email!", Toast.LENGTH_SHORT).show();
                                }

                            }
                        });

            }
        });
        Btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                fStore.collection("users").document(userId).delete()
                        .addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(getActivity(), "User has been deleted!", Toast.LENGTH_LONG).show();

                                    adapter.notifyDataSetChanged();
                                }else{
                                    Toast.makeText(getActivity(), "Failed to delete user!", Toast.LENGTH_SHORT).show();
                                }

                            }
                        });

            }
        });

        return v;
    }
    @Override
    public void onItemClick(DocumentSnapshot snapshot, int position) {
        UserInfo userInfo = snapshot.toObject(UserInfo.class);
        Log.d("Item_CLICK", "Clicked the item : " + position + "and the ID:" + userInfo.getEmail());
        Log.d("Item_CLICK", "Clicked the item : " + position + "and the ID:" + snapshot.getId());
        this.userId = snapshot.getId();
        this.email = userInfo.getEmail();
        this.position = position;
    }
    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();

    }
    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

1 个答案:

答案 0 :(得分:0)

由于此行发生错误:

Query query = fStore.collection("users").document(uid).collection("Detail");

这意味着uid为空或为空。

可能应该是:

uid = fAuth.getInstance().getCurrentUser().getUid();

您可能还想在尝试读取用户的UID之前检查是否已登录的用户:

if (fAuth.getInstance().getCurrentUser() <> null) {
    uid = fAuth.getInstance().getCurrentUser().getUid();
    Query query = fStore.collection("users").document(uid).collection("Detail");        
    ...