我不知道为什么我的限制因为第一次不起作用。我已经关注了Firebase文档。
databaseReference = FirebaseDatabase.getInstance().getReference();
Query queryRef = databaseReference.orderByValue().limitToFirst(6);
queryRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
WallpaperInfo userId = ds.getValue(WallpaperInfo.class);
list.add(userId);
oldestPostId = ds.getKey();
}
Log.d("list",String.valueOf(list.size()));
adapter = new WallpaperGridAdapter(rootView.getContext(),list);
gridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
adapter.notifyDSC();
}
...
};
我在列表中收到所有数据 我的JSON数据(虚拟)
{
"walls" : {
"-L7miwY988GOdQKhSYZp" : {
"artistID" : 141,
"category" : "Luffy",
"downloads" : 1421,
"favs" : 71,
"picURL" : "https://firebasestorage.googleapis.com/v0/b/onepiecewallpaper-5e0c5.appspot.com/o/walls%2Fimage%3A87242?alt=media&token=e1654c8d-8be4-465a-a8cd-cb9075716824",
"sets" : 7877,
"tags" : [ "one piece", "Luffy" ],
"views" : 9898
},
.... }
我正在记录密钥,它给我输出: -
03-18 11:11:59.893 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7miwY988GOdQKhSYZp
03-18 11:11:59.894 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7mjSINcbL_7N7BPgP1
03-18 11:11:59.895 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7ms78xdhps8LPTfWnp
03-18 11:11:59.896 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7msI6VC5jzOA9489mO
03-18 11:11:59.896 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7msbEs4SZwlgxgLiTu
03-18 11:11:59.897 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7nq2pAl9XEQwvT-ad9
03-18 11:11:59.898 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCUSlOo4sz974sL1o
03-18 11:11:59.899 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCZ8u-j2aXacDN3az
03-18 11:11:59.899 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCbQfRSmv00AWaFWi
03-18 11:11:59.900 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCfAfEecc_4NG-0Ir
03-18 11:11:59.901 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCjU90zMgbcnvi-Cz
03-18 11:11:59.902 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCp5fI5HpJs6cU_Fw
03-18 11:11:59.902 11733-11733/we_pirate.one.com.onepiecewallpaper D/keys: -L7oCvejzGo8e-QHtYWz
03-18 11:11:59.902 11733-11733/we_pirate.one.com.onepiecewallpaper D/list: 13
答案 0 :(得分:0)
您需要更改
databaseReference = FirebaseDatabase.getInstance().getReference();
到databaseReference = FirebaseDatabase.getInstance().getReference().child("walls")
然后在onChildAdded
中使用dataSnapshot.getValue(WallpaperInfo.class);
而不是for循环。