当前位置: 首页 > 知识库问答 >
问题:

我不能在onBingViewHolder方法中使用getRef,我试图在另一个视图中显示选定的项

宋望
2023-03-14

我试图在另一个视图中显示选定的项,但我无法使用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;
    }
}

}

共有1个答案

松亦
2023-03-14

正如我所看到的,ImageAdapter类扩展了RecycerView.Adapter。也就是说,您将得到以下错误:

无法解析“ImageAdapter”中的方法“Get Ref”。

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

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

但是,只有当ProductKey作为Upload类中的字段时,这才会起作用。如果还没有,请添加它,并在向数据库添加数据时填充它。

 类似资料: