首页 > 解决方案 > 我不能在 onBingViewHolder 方法中使用 getRef,我正在尝试在另一个视图中显示所选项目

问题描述

我正在尝试在另一个视图中显示所选项目,但我无法使用 getRef 方法获取密钥。我应该如何获得传递数据的密钥?我试图在 StackOverflow 上找到类似的解决方案,但找不到它......

这是我的 onBindViewHolder:

 @Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
    final String productKey = getRef(position).getKey();

    holder.textViewName.setText(uploadCurrent.getName());
    //Picasso.with(mContext).load(uploadCurrent.getmImageUrl()).fit().centerCrop().into(holder.imageView);
    Glide.with(mContext).asBitmap().load(uploadCurrent.getmImageUrl()).into(holder.imageView);

    holder.v.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
      
            Intent i = new Intent(mContext, SingleProductActivity.class);
            i.putExtra("ProductKey", productKey);
            mContext.startActivity(i);
        }
    });
}

还有我试图展示上传数据的班级。

String productKey = getIntent().getStringExtra("ProductKey");
    mDatabaseRef.child(productKey).addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            if (snapshot.exists()){
                String productName = snapshot.child("mName").getValue().toString();
                String productDesc = snapshot.child("mDescription").getValue().toString();
                String imageUrl = snapshot.child("mImageUrl").getValue().toString();

                productDescriptionTxt.setText(productDesc);
                singleProductNameTxt.setText(productName);
                productPriceTagTxt.setText("100");
                Glide.with(mContext).asBitmap().load(imageUrl).into(singleProductImg);
            }
        }

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

        }
    });

这是我的整个适配器

public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private Context mContext;
private List<Upload> mUploads;
private String link;
private DatabaseReference mDatabaseRef;


public ImageAdapter(Context context, List<Upload> uploads) {
    this.mContext = context;
    this.mUploads = uploads;
}

@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(mContext).inflate(R.layout.product_item, parent, false);
    return new ImageViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
    Upload uploadCurrent = mUploads.get(position);
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
    final String productKey = getRef(position).getKey();

    holder.textViewName.setText(uploadCurrent.getName());
    //Picasso.with(mContext).load(uploadCurrent.getmImageUrl()).fit().centerCrop().into(holder.imageView);
    Glide.with(mContext).asBitmap().load(uploadCurrent.getmImageUrl()).into(holder.imageView);

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

            Intent i = new Intent(mContext, SingleProductActivity.class);
            i.putExtra("ProductKey", productKey);
            mContext.startActivity(i);
        }
    });
}


@Override
public int getItemCount() {
    return mUploads.size();
}

public class ImageViewHolder extends RecyclerView.ViewHolder{
    public TextView textViewName;
    public ImageView imageView;
    private CardView parent;
    private View v;

    public ImageViewHolder(@NonNull View itemView) {
        super(itemView);

        parent = itemView.findViewById(R.id.parent);
        textViewName = itemView.findViewById(R.id.productNameTxt);
        imageView = itemView.findViewById(R.id.productImg);
        //v označava jedan prikazan item
        v = itemView;
    }
}

}

标签: javaandroidfirebasefirebase-realtime-databasegoogle-cloud-platform

解决方案


如我所见,您的 ImageAdapter 类扩展了RecyclerView.Adapter。话虽如此,您会收到以下错误:

无法解析“ImageAdapter”中的方法“getRef”。

因为 RecyclerView.Adapter 类内部不包含任何 getRef() 方法,因此出现错误。但是,FirebaseRecyclerAdapter可以。如您所见, getRef()方法存在于 FirebaseRecyclerAdapter 类中,而不存在于 RecyclerView.Adapter 类中。

要解决这个问题,您要么适应Firebase-UI库的代码,要么继续使用 RecyclerView.Adapter 并像这样获取“productKey”:

Upload uploadCurrent = mUploads.get(position);
String productKey = uploadCurrent.getProductKey();

但这只有productKey在类中有 as 字段时才有效Upload。如果您还没有它,请在将数据添加到数据库时添加它并填充它。


推荐阅读