每当用户上传图片并将其发布到Feed中时,我都会使用recyclerview。用户可以单击提要中的图像。但是问题是我得到了错误的数据。我想在此处显示说明,上传者名称等(请参见“上传”下的firebase数据库)
mFirebaseDatabase.getReference("uploads/" + getIntent().getStringExtra("uploadID")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// ...
for (DataSnapshot childSnapshot: dataSnapshot.getChildren()){
tvUserInfo.setText(String.valueOf(dataSnapshot.child("desc").getValue()));
tvUserDesc.setText(String.valueOf(dataSnapshot.child("mName").getValue()));
}
在这里我在ExtraupID上放置了额外的内容
@Override
public void onItemClick(int position) {
Toast.makeText(getContext(), "Normal Click at position" + position, Toast.LENGTH_SHORT).show();
String uploadId = mDatabaseRef.push().getKey();
Intent intent = new Intent(getContext(), BookProfile.class)
.putExtra("uid", mUploads.get(position).getUserId())
.putExtra("imagengpost", mUploads.get(position).getmImageUrl())
.putExtra("title", mUploads.get(position).getmName())
.putExtra("desc", mUploads.get(position).getDesc())
.putExtra("uploadID", uploadId);
Toast.makeText(getActivity(), "Testing uploadID : " + uploadId, Toast.LENGTH_SHORT).show();
startActivity(intent);
}
答案 0 :(得分:0)
您正在向/uploads/$uploadid
添加一个侦听器,以便该侦听器读取单个上载的快照。在这种情况下,snapshot
中的onDataChange
是一次上传,因此您无需循环访问snapshot.getChildren()
。
SoL
mFirebaseDatabase.getReference("uploads/" + getIntent().getStringExtra("uploadID")).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tvUserInfo.setText(String.valueOf(dataSnapshot.child("desc").getValue()));
tvUserDesc.setText(String.valueOf(dataSnapshot.child("mName").getValue()));
}
请注意,如果您要做有多个孩子,您的代码也会被破坏,因为您正在循环中访问dataSnapshot
,而您可能希望{{ 1}}:
childSnapshot