• 8
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

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

      • 1
    • @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

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.

  • 1
Reply Report