我正在尝试使用Hashmap填充我的gridview,但我遇到了这个问题:
使用此代码从firebase获取数据(书籍信息)并填充我的gridview我得到一个空的网格视图:
mDatabasee = FirebaseDatabase.getInstance().getReference();
mDatabasee.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.child("Items").getChildren()) {
Item person = postSnapshot.getValue(Item.class);
Log.d("picture",person.getBookImage());
hm.put(postSnapshot.getKey().toString(),person.getBookImage());
String number = String.valueOf(hm.size());
Log.d("count",number);
此Log.d返回计数:1与我在firebase中的完全相同
}
for(Map.Entry m:hm.entrySet()){
System.out.println(m.getKey()+" "+m.getValue());
}
}
public void onCancelled(DatabaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
String number = String.valueOf(hm.size());
Log.d("css css css",number);
gridView.setAdapter(new ImageAdapter(this, hm));
最后一次Log.d返回 “css css css 0” 所以我收到一个空的网格视图是正常的,以确保我手动添加了一本书,以便新代码成为:
hm.put("-KWyZJM9GhhTe1PzuJMh","http://books.google.com/books/content?id=aJeTCgAAQBAJ&printsec=frontcover&img=1&zoom=1&source=gbs_api");
String number = String.valueOf(hm.size());
Log.d("css css css",number);
gridView.setAdapter(new ImageAdapter(this, hm));
答案 0 :(得分:1)
您必须通知适配器更改。
adapter = new ImageAdapter(this, hm); // Stored as member variable
gridView.setAdapter(adapter);
在firebase方法中,adapter.notifyDataSetChanged();