如何显示子对象с在firestore中导航到特定文档时收集

cnjp1d6j  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(354)

我有一个关于firestore的收藏里面有文档,它们都显示在 RecyclerView 在我的应用程序中,如何使其在单击此文档时打开其子集合?
单击文档时,会发生以下情况:

private FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    private final CollectionReference collectionReference = firebaseFirestore.collection("Items");

    AdapterFactoryKeysSnap adapterFactoryKeysSnap;
    RecyclerView rv_keys_factory;
    RecyclerView.LayoutManager RLM_keys_factory;
    ProgressBar pb_keys_factory;

    private void RecyclerViewFactory() {
        Query query = collectionReference.orderBy("TurnKeyFactory", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<ItemKeys> firestoreRecyclerOptions = new FirestoreRecyclerOptions.Builder<ItemKeys>()
                .setQuery(query, ItemKeys.class).build();

        adapterFactoryKeysSnap = new AdapterFactoryKeysSnap((ClickKeysBlanc) getContext(), getContext(), firestoreRecyclerOptions);
        rv_keys_factory = requireView().findViewById(R.id.rv_keys_factory);
        RLM_keys_factory = new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false);
        SnapHelper snapHelper = new GravitySnapHelper(Gravity.START);
        snapHelper.attachToRecyclerView(rv_keys_factory);
        rv_keys_factory.setHasFixedSize(false);
        rv_keys_factory.setLayoutManager(RLM_keys_factory);
        rv_keys_factory.setNestedScrollingEnabled(true);
        rv_keys_factory.setAdapter(adapterFactoryKeysSnap);
        adapterFactoryKeysSnap.startListening();
        pb_keys_factory = requireView().findViewById(R.id.pb_keys_factory);
        Objects.requireNonNull(rv_keys_factory.getAdapter()).setStateRestorationPolicy(RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY);
        pb_keys_factory.setVisibility(View.GONE);
    }

应用程序了解由于“turnkeyfactory”而单击的文档。在这个文件里面是一个子文件с集合称为“钥匙打开”。
我找到了一种方法来实现这个文档中的构想,我写了以下内容
“工厂退出密钥(工厂)”-名称文档

private final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    private final CollectionReference collectionReference = firebaseFirestore.collection("Items").document("Factory exit key(Factory)").collection("TheKeyOpens");

整个问题是,在我写了这个之后,它出现在主集合的所有文档中,而不是特定的选定文档中。
我认为,如果我的应用程序通过“turnkeyfactory”打开所选文档,如果我这样写,它将工作:

private final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
    private final CollectionReference collectionReference = firebaseFirestore.collection("Items").document("TurnKeyFactory").collection("TheKeyOpens");

但没用。帮我解决问题。
需要

Items->
    - Factory exit key(Factory)->
        - TheKeyOpens->
            -TheKeyOpens 1
            -TheKeyOpens 2
            -TheKeyOpens 3
    - TurnKeyFactory: “1”

    - Factory exit key(Customs)->
        - TheKeyOpens->
            -TheKeyOpens 1
            -TheKeyOpens 2
            -TheKeyOpens 3
    - TurnKeyFactory: “2”


hmmo2u0o

hmmo2u0o1#

我认为,如果我的应用程序通过“turnkeyfactory”打开所选文档,如果我这样写,它将工作:

private final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
private final CollectionReference collectionReference = firebaseFirestore
    .collection("Items")
    .document("TurnKeyFactory")
    .collection("TheKeyOpens");

此引用不起作用,因为向document()方法传递的id不正确。文档的id应该是屏幕截图中显示的“factory exit key(factory)”,而不是实际添加的“turnkeyfactory”。为了使其正常工作,请使用以下参考资料:

private final CollectionReference collectionReference = firebaseFirestore
    .collection("Items")
    .document("Factory exit key(Factory)")
    .collection("TheKeyOpens");

相关问题