无法显示从之前检索到的键下从 Firebase 检索到的 RecyclerView 中的所有数据集
Unable to show all sets of data in RecyclerView retrieved from Firebase from under the keys which are retrieved before
在我的代码中,我检索了一些存储在引用下的 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 个不同的数据集?
用下面的代码替换上面给定的代码对我来说完成了工作:
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
保持不变。
在我的代码中,我检索了一些存储在引用下的 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 个不同的数据集?
用下面的代码替换上面给定的代码对我来说完成了工作:
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
保持不变。