变量'viewholder','postion'&模型是从内部类访问它需要声明最终,但当我使它最终它在适配器中显示错误。我正在使用Firebase 11.2.0请帮助我
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>()
{
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder viewHolder,int position, @NonNull Food model) {
viewHolder.food_name.setText(model.getName());
viewHolder.food_price.setText(String.format(" Rs. %s", model.getPrice().toString()));
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
//ADD FAvorites
if (localdb.isFavorite(adapter.getRef(position).getKey()))
viewHolder.fav_image.setImageResource(R.drawable.ic_favorite_black_24dp);
//Click to change favorite state
viewHolder.fav_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!localdb.isFavorite(adapter.getRef(position).getKey()))
{
localdb.addToFavorites(adapter.getRef(position).getKey());
viewHolder.fav_image.setImageResource(R.drawable.ic_favorite_black_24dp);
Toast.makeText(FoodList.this, "" + model.getName() + "was Added To Favorites", Toast.LENGTH_SHORT).show();
} else {
localdb.removeFromFavorites(adapter.getRef(position).getKey());
viewHolder.fav_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
Toast.makeText(FoodList.this, "" + model.getName() + " was Removed From Favorites", Toast.LENGTH_SHORT).show();
}
}
});
答案 0 :(得分:0)
您需要在此方法中发送 firebase 选项。
因为FirebaseRecyclerAdapter
中的构造函数。
有关详细信息,请参阅以下代码:
public FirebaseRecyclerAdapter(@NonNull FirebaseRecyclerOptions<T> options) {
mSnapshots = options.getSnapshots();
if (options.getOwner() != null) {
options.getOwner().getLifecycle().addObserver(this);
}
}
也请参阅this link。
答案 1 :(得分:0)
无需宣布最终使用位置holder.getAdapterPosition()
它将返回该位置。
对于ViewHolder
,将setOnClickListener移至ViewHolder
类
private void loadListFood(String categoryId) {
adapter=new FirebaseRecyclerAdapter<Food, FoodViewHolder>()
{
@Override
protected void onBindViewHolder(@NonNull FoodViewHolder viewHolder,int position, @NonNull Food model) {
viewHolder.food_name.setText(model.getName());
viewHolder.food_price.setText(String.format(" Rs. %s", model.getPrice().toString()));
Picasso.with(getBaseContext()).load(model.getImage()).into(viewHolder.food_image);
//ADD FAvorites
if (localdb.isFavorite(adapter.getRef(viewHolder.getAdapterPosition()).getKey()))
viewHolder.fav_image.setImageResource(R.drawable.ic_favorite_black_24dp);
}
}
}
class FoodViewHolder extends RecyclerView.ViewHolder {
ImageView fav_image;
FoodViewHolder(View itemView) {
super(itemView);
fav_image = itemView.findViewById(R.id.fav_image);
fav_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (!localdb.isFavorite(adapter.getRef(getAdapterPosition()).getKey()))
{
localdb.addToFavorites(adapter.getRef(getAdapterPosition()).getKey());
fav_image.setImageResource(R.drawable.ic_favorite_black_24dp);
Toast.makeText(FoodList.this, "" + model.getName() + "was Added To Favorites", Toast.LENGTH_SHORT).show();
} else {
localdb.removeFromFavorites(adapter.getRef(getAdapterPosition()).getKey());
fav_image.setImageResource(R.drawable.ic_favorite_border_black_24dp);
Toast.makeText(FoodList.this, "" + model.getName() + " was Removed From Favorites", Toast.LENGTH_SHORT).show();
}
}
});
}