在我的代码中,我正在检索存储在引用下的少量keys
,然后使用这些键我正在检索数据库中存储在这些keys
下的数据。
这是我的代码:
mDatabase.child("child").child(uid).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
idOfGP = dataSnapshot.getValue().toString();
mDatabase.child("anotherChild").child(idOfGP).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
pNames.add(mapData.get("pName"));
pUrls.add(mapData.get("pUrl"));
mDatabase.child("yetAnotherChild").child(idOfGP).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
pS = mapData.get("s").trim();
pV = mapData.get("v").trim();
pW = mapData.get("w").trim();
prepareData();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
...
...
} else {
Toast.makeText(getBaseContext(), "No data here", Toast.LENGTH_SHORT).show();
}
}
...
...
});
这是prepareData()
:
public void prepareData() {
rModelClass = new RModelClass(pS, pNames.get(pNames.size()-1), pUrls.get(pUrls.size()-1), pV, pW);
fastItemAdapter.add(0, rModelClass);
rRV.setAdapter(fastItemAdapter);
rRV.smoothScrollToPosition(0);
}
这是RModelClass.java
:
public class RModelClass extends AbstractItem<RModelClass, RModelClass.ViewHolder> {
String pS, pWith, pV, pW, pUrl;
public RModelClass() {}
public RModelClass(String pS, String pWith, String pUrl, String pV, String pW) {
this.pS = pS.trim() + " with";
this.pWith = pWith.trim();
this.purl = pUrl;
this.pV = pV.trim();
this.pW = pW.trim();
}
@Override
public int getType() {
return R.id.recycler_view_r_p;
}
@Override
public int getLayoutRes() {
return R.layout.r_p_layout;
}
@Override
public void bindView(RModelClass.ViewHolder holder, List payloads) {
super.bindView(holder, payloads);
holder.pS.setText(pS);
holder.pWith.setText(pWith);
holder.pV.setText(pV);
holder.pW.setText(pW);
holder.pUrl.setText(pUrl);
}
protected static class ViewHolder extends RecyclerView.ViewHolder {
TextView pS, pWith, pV, pW, pUrl;
public ViewHolder(View itemView) {
super(itemView);
pS = (TextView) itemView.findViewById(R.id.p_s);
pWith = (TextView) itemView.findViewById(R.id.p_with);
pV = (TextView) itemView.findViewById(R.id.p_v);
pW = (TextView) itemView.findViewById(R.id.p_w);
pUrl = (TextView) itemView.findViewById(R.id.p_url);
}
}
}
参考:mDatabase.child("child").child(uid)...
中存储了3个不同的keys
,但正在发生的事情不是在recyclerview rRV
中显示3组不同的数据,而是3组相同的数据正在显示。
我做错了什么,如何根据从该参考中检索到的3个不同的密钥显示3组不同的数据呢?
答案 0 :(得分:0)
用下面的代码替换上面给出的代码为我完成了这项工作:
mDatabase.child("child").child(uid).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot.getValue() != null) {
idOfGP = dataSnapshot.getValue().toString();
mDatabase.child("anotherChild").child(idOfGP).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
pNames = mapData.get("pName").trim();
pUrls = mapData.get("pUrl");
}
...
...
});
mDatabase.child("yetAnotherChild").child(idOfGP).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Map<String, String> mapData = (Map<String, String>) dataSnapshot.getValue();
pS = mapData.get("s").trim();
pV = mapData.get("v").trim();
pW = mapData.get("w").trim();
prepareData();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
} else {
Toast.makeText(getBaseContext(), "No recently played sport yet!", Toast.LENGTH_SHORT).show();
}
}
...
...
});
此处已更新prepareData()
:
public void prepareData() {
rModelClass = new RModelClass(pS, pNames, pUrls, pV, pW);
fastItemAdapter.add(0, rModelClass);
rRV.setAdapter(fastItemAdapter);
rRV.smoothScrollToPosition(0);
}
RModelClass.java
保持不变。