java - 我不能在 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;
}
}
}
解决方案
如我所见,您的 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
。如果您还没有它,请在将数据添加到数据库时添加它并填充它。
推荐阅读
- php - 如何在 cakephp 中使用 php require
- php - PHP 如果大于
- azure-service-fabric - Service Fabric 应用程序运行状况检查
- powershell - 将记录添加到 Listview 表单事件
- sql - 如何更有效地散列字符串?md5 与 sha256
- swift - Swift - 按下按钮时不播放音频。为什么?
- haskell - 与gameworld一起在main函数中启动Euterpea Music?(在哈斯克尔)
- indexing - 使用空间填充曲线的空间和时空索引
- matlab - 采样频率与低通的最佳比率
- ios - 无密码电子邮件身份验证