//これはリサイクラービューコードです。エラーはAndroid RecyclerView:java.lang.NullPointerException:CartRecyclerAdapter $ CustomViewHolderです。 nullのオブジェクト参照のgetAdapterPosition()」
public class CartRecyclerAdapter extends RecyclerView.Adapter<CartRecyclerAdapter.CustomViewHolder> {
//this is declaration declaration of variables
private List<Cart> feedItemList;
private Context mContext;
private List<Cart> mDataset;
List<Cart> list = Collections.emptyList();
public CartRecyclerAdapter(Context context, List<Cart> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cart_card, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final CustomViewHolder customViewHolder, int i) {
final Cart feedItem = feedItemList.get(i);
if (customViewHolder.mealTitle!= null) {
customViewHolder.mealTitle.setText(feedItem.getMeal_title());
}
if (customViewHolder.mealPrice!= null) {
customViewHolder.mealPrice.setText(feedItem.getMeal_quanitity());
}
if (customViewHolder.mealQuantity!= null) {
customViewHolder.mealQuantity.setText(feedItem.getMeal_price());
}
//エラーは、このラインCustomViewHolder holder = (CustomViewHolder) view.getTag();
がnullを配っているので、あなたは、nullポインタ例外を取得している、ここで
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
CustomViewHolder holder = (CustomViewHolder) view.getTag();
int position = holder.getAdapterPosition();
//check here This is where the error os coming from
String numbers=feedItemList.get(position).toString();
feedItemList.remove(numbers);
}
};
customViewHolder.delete.setOnClickListener(clickListener);
customViewHolder.mealTitle.setTag(customViewHolder);
customViewHolder.mealPrice.setTag(customViewHolder);
customViewHolder.mealQuantity.setTag(customViewHolder);
setAnimation(customViewHolder, i);
}
//Method to remove but I don't know how to initialize.
public void removeAt(int position) {
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,mDataset.size());
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
public class CustomViewHolder extends RecyclerView.ViewHolder {
protected CardView t;
protected ImageView thumbnail;
protected TextView mealTitle;
protected ImageView delete;
protected TextView mealPrice;
protected TextView mealQuantity;
public CustomViewHolder(View v) {
super(v);
t = (CardView) v.findViewById(R.id.card_view);
thumbnail = (ImageView) v.findViewById(R.id.thumbnail);
mealTitle = (TextView)v.findViewById(R.id.meal_title);
delete = (ImageView)v.findViewById(R.id.delete);
mealPrice = (TextView)v.findViewById(R.id.meal_price);
mealQuantity = (TextView)v.findViewById(R.id.meal_quantity);
}
}
}