FATAL EXCEPTION: main. Error creating a Recyclerview adapter

3

I am creating a separate adapter and holder to display three data in a Recyclerview. But when I run the application I get an error.

Adapter:

public class AdapterProd extends RecyclerView.Adapter<HolderProd> {
private ArrayList<ItemsProd> datos;

AdapterProd(ArrayList<ItemsProd> datos){
    this.datos = datos;
}

@Override
public HolderProd onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_list,parent,false);
    return new HolderProd(view);
}

@Override
public void onBindViewHolder(HolderProd holder, int position) {
    holder.etiCodigo.setText(this.datos.get(position).getCodigo());
    holder.etiNombre.setText(this.datos.get(position).getNombre());
    holder.etiValor.setText(this.datos.get(position).getValor());
}

@Override
public int getItemCount() {
    return this.datos.size();
}

Holder:

public class HolderProd extends RecyclerView.ViewHolder {
TextView etiCodigo,etiNombre,etiValor;


public HolderProd(View itemView) {
    super(itemView);
    etiCodigo=(TextView) itemView.findViewById(R.id.prod_cod);
    etiNombre=(TextView) itemView.findViewById(R.id.prod_nombre);
    etiValor=(TextView) itemView.findViewById(R.id.prod_precio);
}

Master Record Class:

public class Registro extends AppCompatActivity {
    private ArrayList<ItemsProd> listaProd;
    private RecyclerView recycler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registro);
        recycler= (RecyclerView)findViewById(R.id.rec_prod);
        recycler.setHasFixedSize(true);
        recycler.setLayoutManager(new GridLayoutManager(this,1));
        ArrayList<ItemsProd> datosProd=new ArrayList<>();
        datosProd.clear();

            for (int i=0; i<50;i++) {
                datosProd.add(new ItemsProd("1","Galleta","0.50"));
            }
        AdapterProd adapter = new AdapterProd(datosProd);
        recycler.setAdapter(adapter);
    }
}

XML where I create the TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/prod_precio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="NamespaceTypo">

    <TextView
        android:id="@+id/prod_precio"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_weight="1"
        android:text="@string/prod_precio" />

    <TextView
        android:id="@+id/prod_nombre"
        android:layout_width="493dp"
        android:layout_height="69dp"
        android:layout_weight="1"
        android:text="@string/prod_nombre"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="83dp" />

    <TextView
        android:id="@+id/prod_cod"
        android:layout_width="0dp"
        android:layout_height="123dp"
        android:layout_weight="1"
        android:text="@string/prod_cod" />

</RelativeLayout>

The error that presents me when executing is the following:

    
asked by lapm 25.09.2018 в 18:29
source

2 answers

0

ClassCastException means you're trying to convert a class from one type to another type of which there is no inheritance relationship between the two.

In this case you are getting the reference of a TextView with id prod_precio ...

 etiValor=(TextView) itemView.findViewById(R.id.prod_precio);

... but in your layout as well as a TextView with id prod_precio there is a RelativeLayout with the same id which you are trying to define as TextView !

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/prod_precio"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="NamespaceTypo">

Change the id of your RelativeLayout so that it does not cause this problem. For example layout_registro , you can also not assign an id to this layout if you do not need it:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/layout_registro"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="NamespaceTypo">

    <TextView
        android:id="@+id/prod_precio"
        android:layout_width="match_parent"
        android:layout_height="65dp"
        android:layout_weight="1"
        android:text="@string/prod_precio" />

    <TextView
        android:id="@+id/prod_nombre"
        android:layout_width="493dp"
        android:layout_height="69dp"
        android:layout_weight="1"
        android:text="@string/prod_nombre"
        tools:layout_editor_absoluteX="40dp"
        tools:layout_editor_absoluteY="83dp" />

    <TextView
        android:id="@+id/prod_cod"
        android:layout_width="0dp"
        android:layout_height="123dp"
        android:layout_weight="1"
        android:text="@string/prod_cod" />

</RelativeLayout>
    
answered by 26.09.2018 в 00:43
0

The problem is that you have 2 widget with the same id in this case the TextView and the RelativeLayout have the following id android:id="@+id/prod_precio"

Only change the id of one of the 2 widgets

I hope you find it useful to welcome you.

    
answered by 25.09.2018 в 19:45