如何从uploadID检索数据取决于上传图片的用户ID?

时间:2019-01-08 09:01:25

标签: java android firebase firebase-realtime-database

每当用户上传图片并将其发布到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);


}

Firebase实时数据库 enter image description here

1 个答案:

答案 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