0

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.

Măria
  • 21
  • 4
  • 1
    what do you mean by the app keeps stopping ? Does it crash? – because_im_batman Apr 24 '20 at 09:12
  • Yes. It says has stopped and suggest to open the app again. – Măria Apr 24 '20 at 09:15
  • 2
    It's very difficult to debug a crash without a stack trace. See [Unfortunately MyApp has stopped. How can I solve this?](//stackoverflow.com/questions/23353173) for Android-specific advice, and [What is a stack trace, and how can I use it to debug my application errors?](//stackoverflow.com/questions/3988788) for advice on what to do once you have the stack trace. If you still need help, edit your question to include the **complete stack trace**, as well as **which line of your code** the stack trace points to. – Ryan M Apr 24 '20 at 09:15

1 Answers1

0

you are setting Int value instead of String value to TextView and you will get ResourceNotFoundException:

@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);//ERROR change to q.setText("" + qty);
}

you have same problem in decrementQ

ygngy
  • 3,630
  • 2
  • 18
  • 29