如何使用菜单充气器更改 Firestore Collection?

how to change Firestore Collection with Menu inflator?

我的 Cloud Firestore 中有一些不同的集合,

  1. 项目列表 1
  2. 项目列表 2
  3. ++

我想用菜单项切换它们,但是在 'firestore.collection' 我只坐了 ProjectList1,现在我该如何设置或切换 ProjectList2 或项目列表 3 在 firestore.collection(ProjectList1)..

代码如下:

mFirestore = FirebaseFirestore.getInstance();

    mFirestore.collection("ProjectList1").addSnapshotListener(new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

            if(e !=null){
                Log.d(TAG,"Error :"+ e.getMessage());
            }
            for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                if (doc.getType()==DocumentChange.Type.ADDED){
                    Prjs prjs = doc.getDocument().toObject(Prjs.class);
                    prjList.add(prjs);
                   csListAdapter.notifyDataSetChanged();


                }
            }
        }
    });

我尝试切换到菜单,但无法正常工作,它首先保留所有以前的数据..

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

         case R.id.pro1:
 mFirestore = FirebaseFirestore.getInstance();

mFirestore.collection("ProjectList1").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

        if(e !=null){
            Log.d(TAG,"Error :"+ e.getMessage());
        }
        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

            if (doc.getType()==DocumentChange.Type.ADDED){
                Prjs prjs = doc.getDocument().toObject(Prjs.class);
                prjList.add(prjs);
               csListAdapter.notifyDataSetChanged();

            }
        }
    }
});
        return true;

        case R.id.pro2:
mFirestore = FirebaseFirestore.getInstance();

mFirestore.collection("ProjectList2").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

        if(e !=null){
            Log.d(TAG,"Error :"+ e.getMessage());
        }
        for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

            if (doc.getType()==DocumentChange.Type.ADDED){
                Prjs prjs = doc.getDocument().toObject(Prjs.class);
                prjList.add(prjs);
               csListAdapter.notifyDataSetChanged();


            }
        }
    }
});
         return true;
            default:
}        return super.onOptionsItemSelected(item); }

您需要先清除现有列表中的数据,然后再从 Firebase 添加更多数据

prjList.clear()