私はGoogleのFirebaseに新たなんだと私はそれについて何かを学ぶことをしようとしています。私は、「グループページ」で、あなたはリストビューにすべてのグループを見ることができ、あなたは人のグループを作成することができるAndroidアプリをやってgroup..thenのタイトルを設定しています。私のfirestoreデシベルの構造は、このようなものです:
ユーザー - > 電子メール(文書) ---> グループ(コレクション) > - GroupNameの(ドキュメント)グループ名の文書がpartecipants ArrayListを(:名1、partecipant1:名前2などpartecipant 0)が含まれています。
私は(グループタイトルである)文書IDとpartecipantsのArrayListのを取得したいと思いますが、私は、コード内の各に対して使用するのかわかりません...
これは私のコードです:
public void load_list_view(){
String email = getEmail();
final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();
docRef.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
titleArray.add(documentSnapshot.getId());
titleString = documentSnapshot.getId();
partecipantsArray.add(documentSnapshot.getString("partecipant"));
num_partecipants = partecipantsArray.size();
numArray.add(num_partecipants);
trash = R.drawable.trash_icon;
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
listView.setAdapter(customAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(GroupActivity.this, e.getStackTrace().toString(), Toast.LENGTH_LONG).show();
}
});
}
でtitleArray.add(documentSnapshot.getId());
、それはランダムなIDを取得し、私は理由を理解することはできません。
私は約インターネット上で十分な文書発見していないArraylist
とfirestoreを。
まず第一に示すように、あなたは違った自分のコードを書く必要があり、コレクション内のすべてのドキュメントを取得するには、このマニュアルを参照して。
db.collection("cities")
.get()
.addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Log.d(TAG, document.getId() + " => " + document.getData());
}
} else {
Log.d(TAG, "Error getting documents: ", task.getException());
}
}
});
第二に、あなたが使うべきArrayListを取得している場合(ArrayList<String>) documentSnapshot.get("key")
の代わりにdocumentSnapshot.getString("key")
。
第三に(下記)のコード行でfirebaseがランダムIDを持つ新しいドキュメント参照を生成しているので、あなたは、ランダムIDを取得しています。参考リンク。
final DocumentReference docRef = db.collection("users").document(email).collection("Group").document();
あなたの助けのために、私はあなたのコードを微調整している、あなたはこのコードを試してみて、それが仕事やいないかどうかを確認することができます。
public void load_list_view() {
String email = getEmail();
final DocumentReference docRef = firestore.collection("users").document(email);
docRef.collection("Group")
.get()
.addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (QueryDocumentSnapshot document : queryDocumentSnapshots) {
//Extracting Group name from each document
titleString = document.getId();
titleArray.add(titleString);
//Extracting participants ArrayList from each document
partecipantsArray.add((ArrayList<String>) document.get("participant"));
numArray.add(num_partecipants);
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
}
num_partecipants = partecipantsArray.size();
numArray.add(num_partecipants);
trash = R.drawable.trash_icon;
firstChar = Character.toString(titleString.charAt(0));
firstCharArray.add(firstChar);
customAdapter = new GroupAdapter(GroupActivity.this, firstCharArray, titleArray, numArray, trash);
listView.setAdapter(customAdapter);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
//HANDLE EXCEPTION
}
});
}