如何从Firebase数据库获取密钥

时间:2020-08-09 14:54:25

标签: android firebase firebase-realtime-database google-cloud-firestore

每次单击recyclerview中的项目时,我想获取特定的密钥。我的代码一切正常,但是我无法获得子键。我只想以字符串形式获取密钥,但是Toast结果是位置0、1、2、3、4等,除了在这里获取密钥之外,其他一切工作正常

Firebase数据库

这是我的活动

private RecyclerView recyclerView;
private FirebaseFirestore firebaseFirestore;
private FirestoreRecyclerAdapter<Upload, SearchListActivity.ImageViewHolder> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_list);

    firebaseFirestore = FirebaseFirestore.getInstance();

    recyclerView = findViewById(R.id.search_list_recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));

    Query query = firebaseFirestore.collection("Dealers");

    FirestoreRecyclerOptions<Upload> options = new FirestoreRecyclerOptions.Builder<Upload>()
            .setQuery(query, Upload.class)
            .build();
    adapter = new FirestoreRecyclerAdapter<Upload, ImageViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final ImageViewHolder holder, final int position, @NonNull final Upload model) {

            holder.mCompanyName.setText(model.getCompanyName());
            Picasso.get().load(model.getImageUrl()).into(holder.dealerAvatar);

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    final DatabaseReference clickedUser = FirebaseDatabase.getInstance().getReference().child("Dealers").child(String.valueOf(position));
                        clickedUser.addListenerForSingleValueEvent(new ValueEventListener() {
                            @Override
                            public void onDataChange(@NonNull DataSnapshot snapshot) {
                                String result = clickedUser.getKey();

                                Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();
                            }

                            @Override
                            public void onCancelled(@NonNull DatabaseError error) {

                            }
                        });
                }
            });
        }

        @NonNull
        @Override
        public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.listview_custom_layout, parent, false);

            return new ImageViewHolder(view);
        }
    };
    recyclerView.setAdapter(adapter);
}

public class ImageViewHolder extends RecyclerView.ViewHolder{
    
    public TextView mCompanyName;
    public ImageView dealerAvatar;
    public View mView;
    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);
        mView = itemView;
        mCompanyName = itemView.findViewById(R.id.company_name);
        dealerAvatar = mView.findViewById(R.id.list_view_avatar);

    }
}

这是我的带有getter和setter的Upload.class

public class Upload {

private String uid; // <<this will be the key, or I just need to get the key even not using this Upload.class I just need the key
private String companyName;
private String imageUrl;

public Upload(String uid){this.uid = uid}


public Upload(){}


public Upload (String uid, String companyName, String imageUrl){
    this.companyName = companyName;
    this.slogan = email;
    this.companyAddress = companyAddress;
    this.imageUrl = imageUrl;
}

1 个答案:

答案 0 :(得分:1)

Sir Frank van Puffelen提出建议-

您可以从documentSnapShot中获取键值-

@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
    String docuementKey = documentSnapshot.getReference().getId();

    //toast message 
    Toast.makeText(getApplicationContext(), docuementKey, Toast.LENGTH_SHORT).show();
}

然后您可以将其保存在您的上载POJO中。

快乐编码!