java获取文档云存储中集合的所有文档
我有一份名单(piloto),每个人都有一些音频收藏。 我将这些音频放在一个列表中,该列表发送适配器并在recyclerview中显示
我尝试了不同的代码,只有这对我有效,但问题在于:
为什么我第一次点击一个项目时会显示音频列表为空?然后返回,下次我单击该项目时,它会正确显示列表。 当我使用真正的smarthphone运行应用程序时,当我第一次使用emulator正确显示列表时,就会发生这种情况
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;
}
}
}
});
}
# 1 楼答案
这是因为您第一次设置适配器时,列表为空
您应该在
EventListener
中填充audios
,并在那里设置/交换适配器,以便在刷新数据时获取数据