[英]Get all the documents of a collection which is in a document Cloud Firestore
我有一個人清單(piloto),每個人都有一個帶有一些音頻的收藏。 我將這些音頻放在發送適配器的列表中,並在recyclerview中顯示。
我嘗試使用不同的代碼,但只有這對我有用,但是問題在這里:
為什么我第一次單擊某個項目時它顯示的音頻列表為空? 然后返回,下次我單擊該項目時,它將正確顯示列表。 當我使用真正的smarthphone運行應用程序時,當我第一次使用模擬器運行它時,它會正確顯示列表。
public class PilotoDetalle extends AppCompatActivity {
RecyclerView rv;
List<Audio> audios;
AdapterAudios adapter;
private Audio audio;
private Piloto piloto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_piloto_detalle);
rv = findViewById(R.id.recyclerAudio);
rv.setLayoutManager(new LinearLayoutManager(this));
audios = new ArrayList<>();
adapter = new AdapterAudios(audios);
rv.setAdapter(adapter);
final FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("pilotos").document(piloto.getId()).collection("audios")
.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot snapshots,
@Nullable FirebaseFirestoreException e) {
if (e != null) {
Log.w("TAG", "listen:error", e);
return;
}
for (DocumentChange dc : snapshots.getDocumentChanges()) {
switch (dc.getType()) {
case ADDED:
Log.d("TAG", "New Msg: " + dc.getDocument().toObject(Message.class));
//audios.removeAll(audios);
Audio audio = dc.getDocument().toObject(Audio.class);
audios.add(audio);
break;
case MODIFIED:
Log.d("TAG", "Modified Msg: " + dc.getDocument().toObject(Message.class));
break;
case REMOVED:
Log.d("TAG", "Removed Msg: " + dc.getDocument().toObject(Message.class));
break;
}
}
}
});
}
發生這種情況的原因是,第一次設置適配器時,該列表為空。
audios = new ArrayList<>();
adapter = new AdapterAudios(audios);
rv.setAdapter(adapter);
您應該在EventListener
填充audios
並在那里設置/交換適配器,以便在刷新時獲取數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.