I have this restaurant app and I want to make a checkout looking like FoodPanda's (if you have the layout in mind). The recyclerview items should contain a textview containing the quantity of one product, surrounded by two buttons for decrement and increment that quantity, then two textviews with product name and the price. I've tried this so far in my adapter class:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<Product> product;
private OnItemClickListener mListener;
public interface OnItemClickListener {
void decrementQ(int position);
void incrementQ(int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
mListener = listener;
}
public static class MyViewHolder extends ViewHolder {
public ImageView incrementQty, decrementQty;
public TextView productName, price, qty;
public MyViewHolder(View itemView, final OnItemClickListener listener) {
super(itemView);
incrementQty = itemView.findViewById(R.id.incrementProduct);
decrementQty = itemView.findViewById(R.id.decrementProduct);
qty = itemView.findViewById(R.id.qtyProduct);
productName = itemView.findViewById(R.id.productName);
price = itemView.findViewById(R.id.productPrice);
decrementQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.decrementQ(position);
}
}
});
incrementQty.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(listener != null){
int position = getAdapterPosition();
if(position != RecyclerView.NO_POSITION)
listener.incrementQ(position);
}
}
});
}
}
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(ArrayList<Product> thisProduct) {
product = thisProduct;
}
// Create new views (invoked by the layout manager)
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.products, parent, false);
MyViewHolder mv = new MyViewHolder(v, mListener);
return mv;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
Product currentProduct = product.get(position);
holder.decrementQty.setImageResource(currentProduct.getDecrementSource());
holder.qty.setText(currentProduct.getQty());
holder.incrementQty.setImageResource(currentProduct.getIncrementSource());
holder.productName.setText(currentProduct.getProductName());
holder.price.setText(currentProduct.getProductPrice());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return product.size();
}
}
And this is my checkout activity class:
public class checkout extends AppCompatActivity {
private ArrayList<Product> product;
private RecyclerView recyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkout);
product = new ArrayList<>();
product.add(new Product(R.drawable.minus, "1", R.drawable.plus, "Buttered Noodles", "15 RON"));
product.add(new Product(R.drawable.minus, "2", R.drawable.plus, "Teryiaki","20 RON"));
product.add(new Product(R.drawable.minus, "3", R.drawable.plus, "ginger", "3 RON"));
buildCheckout();
}
public void removeItem(int position) {
product.remove(position);
mAdapter.notifyItemRemoved(position);
}
public void buildCheckout(){
recyclerView = findViewById(R.id.checkoutList);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
mAdapter = new MyAdapter(product);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
mAdapter.setOnItemClickListener(new MyAdapter.OnItemClickListener() {
@Override
public void decrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty--;
if(qty < 1)
removeItem(position);
else
q.setText(qty);
}
@Override
public void incrementQ(int position) {
TextView q = recyclerView.findViewHolderForAdapterPosition(position).itemView.findViewById(R.id.qtyProduct);
int qty = Integer.parseInt((String) q.getText());
qty++;
q.setText(qty);
}
});
}
}
The problem is, when I try to interact with the increment and decrement buttons, the app keeps stoping and I don't know what should I do next or what I did wrong.