我编写了从Firebase数据库获取密钥并将其写入ListView
组件的代码。有一个我无法解决的简单问题。
我的代码如下。有人可以帮我吗?
List<String> list;
ArrayAdapter<String> adapter;
ListView rooms_list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rooms_list);
rooms_list = findViewById(R.id.rooms_list);
adapter = new ArrayAdapter<>(this, R.layout.rooms_list_layout, list);
DatabaseReference mref = FirebaseDatabase.getInstance().getReference("chatRooms");
mref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
list.add(snapshot.getKey());
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError firebaseError) {
}
});
rooms_list.setAdapter(adapter);
}
}