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());
}
}
});
}
}
});
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.