-1

enter image description hereI am adding plus & minus button in recyclerView but when I run my application then the application has crashed. The error show, resource not found an exception & another error show onBindViewHolder.

I've tried to solve productAdapter onBindViewHolder in RecyclerView.

        <?xml version="1.0" encoding="utf-8"?>
            <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">


                <androidx.cardview.widget.CardView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="5dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal"
                        android:weightSum="12">

                        <androidx.appcompat.widget.AppCompatCheckBox
                            android:id="@+id/checkid"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="1" />

                        <ImageView
                            android:id="@+id/imageView"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_weight="2"
                            android:padding="4dp" />

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="wrap_content"
                            android:layout_weight="9"
                            android:orientation="vertical">

                            <LinearLayout
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView
                                    android:id="@+id/textViewTitleid"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginLeft="5dp"
                                    android:layout_toRightOf="@id/imageView"
                                    android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)"
                                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                                    android:textColor="#000000" />

                                <TextView
                                    android:id="@+id/textViewShortDescid"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_below="@id/textViewTitleid"
                                    android:layout_marginLeft="5dp"
                                    android:layout_marginTop="5dp"
                                    android:layout_toRightOf="@id/imageView"
                                    android:text="13.3 Inch, 256 GB"
                                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

                            </LinearLayout>

                            <!--<TextView-->
                            <!--android:id="@+id/textViewRatingid"-->
                            <!--android:layout_width="wrap_content"-->
                            <!--android:layout_height="wrap_content"-->
                            <!--android:layout_below="@id/textViewShortDescid"-->
                            <!--android:layout_marginLeft="5dp"-->
                            <!--android:layout_marginTop="5dp"-->
                            <!--android:layout_toRightOf="@id/imageView"-->
                            <!--android:background="@color/colorPrimary"-->
                            <!--android:paddingLeft="15dp"-->
                            <!--android:paddingRight="15dp"-->
                            <!--android:text="4.7"-->
                            <!--android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"-->
                            <!--android:textStyle="bold" />-->
                            <RelativeLayout
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content">

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentLeft="true"
                                    android:orientation="horizontal">

                                    <TextView
                                        android:id="@+id/textViewPriceid"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:text="INR 56990"
                                        android:textStyle="bold" />
                                </LinearLayout>

                                <LinearLayout
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_alignParentRight="true"

                                    >

                                    <Button
                                        android:id="@+id/minusbtn"
                                        android:layout_width="35dp"
                                        android:layout_height="35dp"
                                        android:background="@null"
                                        android:text="-"
                                        android:textSize="16dp" />

                                    <TextView
                                        android:id="@+id/counter_text"
                                        android:layout_width="wrap_content"
                                        android:layout_height="wrap_content"
                                        android:background="#DFDDDD"
                                        android:padding="2dp"
                                        android:text="0" />

                                    <Button
                                        android:id="@+id/plusbtn"
                                        android:layout_width="35dp"
                                        android:layout_height="35dp"
                                        android:background="@null"
                                        android:text="+"
                                        android:textSize="16dp" />


                                </LinearLayout>

                            </RelativeLayout>
                        </LinearLayout>

                    </LinearLayout>

                </androidx.cardview.widget.CardView>


            </LinearLayout>



        <?xml version="1.0" encoding="utf-8"?>
        <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MainActivity">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
            </androidx.recyclerview.widget.RecyclerView>

        </androidx.constraintlayout.widget.ConstraintLayout>





                package com.example.mycart;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatCheckBox;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ProductViewHolder> {

    private Context context;
    private List<Product> productList;
    //static int count =0;


    public ProductAdapter(Context context, List<Product> productList) {
        this.context = context;
        this.productList = productList;
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.list, null);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ProductViewHolder holder, int position) {
        final Product product = productList.get(position);
        holder.textViewTitle.setText(product.getTitle());
        holder.textViewDesc.setText(product.getShortdesc());
        // holder.textViewRating.setText(String.valueOf(product.getRating()));
        holder.textViewPrice.setText(String.valueOf(product.getPrice()));
        holder.imageView.setImageDrawable(context.getResources().getDrawable(product.getImage()));
        //holder.counterText.setText(String.valueOf(getItemCount()));

        holder.count.setText(String.valueOf(product.getCount()));
        holder.plusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                product.setCount(product.getCount() + 1);
                holder.count.setText(String.valueOf(product.getCount()));
                //product.setCount(product.getCount() + 1);
               //holder.count.setText(String.valueOf(product.getCount()+1));
                // product.setCount(product.getCount() + 1);
                //holder.count.setText(product.getCount());
            }
        });

        holder.minusbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (product.getCount() > 1) {
                    product.setCount(product.getCount() - 1);
                    holder.count.setText(String.valueOf(product.getCount()));
                    //product.setCount(product.getCount() - 1);
                    //holder.count.setText(String.valueOf(product.getCount()-1));
                    //product.setCount(product.getCount() - 1);
                    //holder.count.setText(product.getCount());
                }
            }
        });
    }

    @Override
    public int getItemCount() {

        return productList.size();
    }


    public class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView imageView;
        TextView textViewTitle, textViewDesc, textViewPrice, count;
        AppCompatCheckBox checkBox;
        Button minusbtn, plusbtn;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.imageView);
            textViewTitle = itemView.findViewById(R.id.textViewTitleid);
            textViewDesc = itemView.findViewById(R.id.textViewShortDescid);
            // textViewRating =itemView.findViewById(R.id.textViewRatingid);
            textViewPrice = itemView.findViewById(R.id.textViewPriceid);
            count = itemView.findViewById(R.id.counter_text);
            checkBox = itemView.findViewById(R.id.checkid);
            minusbtn = itemView.findViewById(R.id.minusbtn);
            plusbtn = itemView.findViewById(R.id.plusbtn);

        }
    }

}



                package com.example.mycart;

                public class Product {
                    private int id;
                    private String title, shortdesc;
                    private double price;
                    private int image;
                    private int count = 1;


                    public Product(int id, String title, String shortdesc, double price, int image) {
                        this.id = id;
                        this.title = title;
                        this.shortdesc = shortdesc;
                        this.price = price;
                        this.image = image;
                    }

                    public void setCount(int count) {
                        this.count = count;
                    }

                    public int getCount() {
                        return count;
                    }

                    public int getId() {
                        return id;
                    }

                    public String getTitle() {
                        return title;
                    }

                    public String getShortdesc() {
                        return shortdesc;
                    }

                    public double getPrice() {
                        return price;
                    }

                    public int getImage() {
                        return image;
                    }

                }







        package com.example.mycart;

        import androidx.appcompat.app.AppCompatActivity;
        import androidx.recyclerview.widget.LinearLayoutManager;
        import androidx.recyclerview.widget.RecyclerView;

        import android.os.Bundle;

        import java.util.ArrayList;
        import java.util.List;

        public class MainActivity extends AppCompatActivity {
            RecyclerView recyclerView;
            ProductAdapter adapter;
            List<Product> productList;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

                productList = new ArrayList<>();
                recyclerView =(RecyclerView) findViewById(R.id.recyclerView);
                recyclerView.setHasFixedSize(true);
                recyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));

                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));
                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));
                productList.add(
                        new Product(
                                1,
                                "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra",
                                "13.3 inch, Silver, 1.35 kg",
                                6500.00,R.drawable.macbook));


                adapter = new ProductAdapter(this,productList);
                recyclerView.setAdapter(adapter);



            }
        }

when I will click a plus button the value increment individually & when I click the minus button the value decrement individually in recyclerView each & every item.

This is my logcat result.

  • Post your `list` layout XML in the question – Rahul Khurana Aug 19 '19 at 04:40
  • yes, I posted list layoutXML in this question – Nasir Uddin Aug 19 '19 at 05:06
  • Post your error log in the question. – Rahul Khurana Aug 19 '19 at 05:10
  • You're calling `setText()` with an `int` in multiple places, not just the ones currently shown in the answer below. You need to fix all of them; e.g., `holder.count.setText(product.getCount());`. – Mike M. Aug 19 '19 at 05:10
  • You still have not changed the three occurrences of `holder.count.setText(product.getCount());` to `holder.count.setText(String.valueOf(product.getCount()));`. And you still have not provided the logs/stack trace. – Mike M. Aug 19 '19 at 05:52
  • android.content.res.Resources$NotFoundException: String resource ID #0x1 – Nasir Uddin Aug 19 '19 at 05:55
  • Please don't post screenshots of logcat output. Please post all text as text. That said, the Exception is pointing to a `setText()` call in `onBindViewHolder()`, on line 46. That looks like `holder.count.setText(product.getCount());`. Again, change that line to `holder.count.setText(String.valueOf(product.getCount()));`. – Mike M. Aug 19 '19 at 06:10
  • android.content.res.Resources$NotFoundException: String resource ID #0x1 , at com.example.mycart.ProductAdapter.onBindViewHolder(ProductAdapter.java:46), at com.example.mycart.ProductAdapter.onBindViewHolder(ProductAdapter.java:17) this is the logcat result. – Nasir Uddin Aug 19 '19 at 06:24
  • Look at the line number in the stack trace: `at com.example.mycart.ProductAdapter.onBindViewHolder(ProductAdapter.java:46)` <-- Line 46. Which line is that? – Mike M. Aug 19 '19 at 06:28
  • The application is running right now but when I click the plus button it only working one time & minus button is not totally working. – Nasir Uddin Aug 19 '19 at 06:40
  • I don't understand what you mean. Is it still crashing? Are you still getting the `Resources$NotFoundException`? – Mike M. Aug 19 '19 at 06:41
  • The application is not crashed but I didn't get exactly result. When I click plus button then its only one time clicked the plus button is not working & minus button is not working totally. – Nasir Uddin Aug 19 '19 at 06:45
  • Well, that's a different question, really, but I will say that you should not have changed the code in the `OnClickListener`s as the answer below suggested. As I said in my comment on that answer, all you had to do was to go back to the code you originally posted, and change those three lines I pointed out. For example, your `plusbtn` should be `product.setCount(product.getCount() + 1);`, `holder.count.setText(String.valueOf(product.getCount()));`. – Mike M. Aug 19 '19 at 06:45
  • 1
    yes, brother.sloved this problem. Thanks a lot for your help. I have a question for you. – Nasir Uddin Aug 19 '19 at 06:56
  • Sure. No problem. What's your question? – Mike M. Aug 19 '19 at 07:03
  • Brother, my question was duplicate ?? not like that because you give a link, that answer is not the same in my question. 4 hours I was trying to solve it. whatever don't mind, thank you so much. – Nasir Uddin Aug 19 '19 at 07:07
  • We mark questions as duplicates for the ultimate issue and solution, not for the overall question or project setup. In this case, your ultimate problem was that you were calling `setText()` with an `int`, which the duplicate question explains and gives the solution for. Though you were having this problem inside a `RecyclerView.Adapter`, inside of a sizable, unique project, none of that was relevant. This is why you need to narrow down your problem first. Please have a look at how to create a [mcve]. Often, just creating one will help you find the issue, without having to post a question here. – Mike M. Aug 19 '19 at 07:14

1 Answers1

1

You are setting integer value TextView, basically integer value in TextView navigate to resource file with that ID. So rather use integer value as it, wrap it with String.

So make below changes in your code on plus button click.

holder.count.setText(String.valueOf(product.getCount() + 1));

And in minus button click.

holder.count.setText(String.valueOf(product.getCount() - 1));
DHAVAL A.
  • 2,251
  • 2
  • 12
  • 27
  • Your answer is not working on my project. – Nasir Uddin Aug 19 '19 at 05:05
  • What is the error log says? Can you please post? – DHAVAL A. Aug 19 '19 at 05:07
  • Based on @Mike's comment, I have fixed my answer. Please check it. – DHAVAL A. Aug 19 '19 at 05:14
  • setCount(int) in Product cannot be applied to (Java.lanf.String) , the red line shown in this line – Nasir Uddin Aug 19 '19 at 05:19
  • @NasirUddin We're getting mixed up, here. Just go back to your original code, and change all three `holder.count.setText(product.getCount());` calls to `holder.count.setText(String.valueOf(product.getCount()));`. You have one directly in `onBindViewHolder()`, and one in each `OnClickListener`. – Mike M. Aug 19 '19 at 05:21
  • your answer is not working. How can I solve it? – Nasir Uddin Aug 19 '19 at 05:34
  • @NasirUddin Edit your question to update the code to what you have now, and to provide the complete [stack trace](https://stackoverflow.com/a/23353174). – Mike M. Aug 19 '19 at 05:37
  • yes, I posted the code to what I have now, Please give me the right solution. – Nasir Uddin Aug 19 '19 at 05:48
  • To completely overcome this error, just wrap up all your `setText` value with `.setText(String.valueOf($your value here))`. – DHAVAL A. Aug 19 '19 at 05:54
  • The application is running but I didn't get the actual result. – Nasir Uddin Aug 19 '19 at 06:47
  • After updating value of your single item using `product.setCount({value})` on button click, you are not updating source list. And that's why it's only consider initial value every time when you click on it. – DHAVAL A. Aug 19 '19 at 06:58