2

I'm actually trying to fetch all document id's from firestore database into array adapter for that i'm using document.getid() but instead of fetching all document id's together it instead fetches them one by one however what i actually want that all id's should be displayed in a list how can i counter this problem any help will be appreciated. Take a look at the code i'm trying to implement.

public void onClick(View v) {
                if (position==0) {
                    db.collection("Listening")
                            .get()
                            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                @Override
                                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                                    if (task.isSuccessful()) {
                                        for (QueryDocumentSnapshot document : task.getResult()) {
                                            if (document != null) {
                                                AlertDialog.Builder builderSingle = new AlertDialog.Builder(mContext);

                                                builderSingle.setTitle("Select A Test");

                                                final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.select_dialog_singlechoice);

                                                //  for (int i=0;i<document.getId().;i++) {

                                           //  }
                                                builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        dialog.dismiss();
                                                    }
                                                });

                                                builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
                                                    @Override
                                                    public void onClick(DialogInterface dialog, int which) {
                                                        String strName = arrayAdapter.getItem(which);
                                                        Intent intent=new Intent(mContext,Listening.class);
                                                        intent.putExtra("tname",strName);
                                                        mContext.startActivity(intent);
                                                    }
                                                });
                                                builderSingle.show();
                                            } else {
                                                Log.d("LOGGER", "No such document");
                                            }
                                        }
                                    } else {
                                        Log.w(TAG, "Error getting documents.", task.getException());
                                    }
                                }
                            });
                    }
            }
        });

This is the screenshot of my database: Database sreenshot

Mr. Patel
  • 1,379
  • 8
  • 21
  • I see that the document is empty. In order to display data, your documents should contain some data. If you are interested, **[this](https://stackoverflow.com/questions/49277797/how-to-display-data-from-firestore-in-a-recyclerview-with-android/49277842)** is a recommended way in which you can retrieve data from a Cloud Firestore database and display it in a `RecyclerView` using `FirestoreRecyclerAdapter`. – Alex Mamo Feb 05 '19 at 10:53
  • @AlexMamo i know how to display data from document but what i want is that all documents within a collection should be fetched into an array adapter – Mr. Patel Feb 05 '19 at 11:18
  • You don't have any data that can be displayed (according to your screenshot). – Alex Mamo Feb 05 '19 at 11:20
  • @AlexMamo you're still not getting it i don't want to fetch data i want to fetch all document names – Mr. Patel Feb 05 '19 at 11:24

1 Answers1

2

According to your comment:

i want to fetch all document names

Please use the following code in order to get all document ids.

db.collection("Listening").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        List<String> ids = new ArrayList<>();
        if (task.isSuccessful()) {
            for (QueryDocumentSnapshot document : task.getResult()) {
                String id = document.getId();
                ids.add(id);
            }
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(mContext, android.R.layout.select_dialog_singlechoice, ids);
        listView.setAdapter(arrayAdapter);
    }
});

The result will be a list that will contain three strings:

Test 1
Test 2
Test 3

Please note that I have created the adapter outside the loop and I have used the ArrayAdapter constructor with three arguments.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193