How to pass this DB to reclyclerviewer - cannot convert from cardview to text view

Daniel Palason :

I'm trying to create a barcode reader and I THINK I already saved the info in the DB, but now I want to show it in another activity. Im getting this error on runtime:

androidx.cardview.widget.CardView cannot be cast to android.widget.TextView

I know the code is a Frankeinstein, but this is for learning.

this is the mainxml

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

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/lista_activity"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/itemlista"/>
</RelativeLayout>

this is the listitem //cardviewer

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="4dp">

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:text="Line 1"
            android:textColor="@android:color/black"
            android:textSize="15sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/textView"

            android:text="Line 2"
            android:textSize="15sp"
            android:textStyle="bold"
            android:layout_marginLeft="8dp" />
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/textView2"
            android:layout_marginStart="8dp"
            android:text="Line 3"
            android:textSize="15sp"
            android:layout_marginLeft="8dp" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

this is the "mainactivity"

        private RecyclerView.Adapter adaptador;
        private RecyclerView.LayoutManager layoutManager;

        private ProdutoDAO dao;
        private ArrayList<Produto> produto;

        public void onCreate(Bundle savedInstanceState) {

            Toast.makeText(this, "hello", Toast.LENGTH_SHORT).show();

            super.onCreate(savedInstanceState);
            setContentView(R.layout.listar_activity);

            listView = findViewById(R.id.lista_activity);
            dao = new ProdutoDAO(this);
            produto = dao.listar();
            layoutManager = new LinearLayoutManager(this);
            listView.setLayoutManager(layoutManager);
            listView.setHasFixedSize(false);
            adaptador = new MyAdapter(produto);
            listView.setAdapter(adaptador);
        }
}

this is the adapter where I get the error

package com.example.barcode;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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


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

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

    private ArrayList<Produto> minhaLista;

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView1;
        public TextView textView2;
        public TextView textView3;

        public MyViewHolder(TextView v) {
            super(v);
            textView1 = v.findViewById(R.id.textView);
            textView2 = v.findViewById(R.id.textView2);
            textView3 = v.findViewById(R.id.textView3);
        }
    }

    public MyAdapter(ArrayList<Produto> x) {
        minhaLista = x;
    }
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //this is where I get shit
        View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlista, parent, false);

        MyViewHolder vh = new MyViewHolder((TextView) V);
        return vh;
    }

    @NonNull 
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

     Produto currentItem = minhaLista.get(position);
     holder.textView1.setText(currentItem.getId());
     holder.textView2.setText(currentItem.getCodigo());
     holder.textView3.setText(currentItem.getQuantidade());
    }

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

and finally this is the DB class

package com.example.barcode;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

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

public class ProdutoDAO {

    private Conexao conexao;
    private SQLiteDatabase banco;

        public ProdutoDAO (Context context){
            conexao = new Conexao(context);
            banco = conexao.getWritableDatabase();
        }

    public long inserir(Produto produto){

        ContentValues values = new ContentValues();
        values.put("codigo", produto.getCodigo());
        values.put("quantidade", produto.getQuantidade());
        return banco.insertOrThrow("produto", null, values);
    }

    public ArrayList<Produto> listar(){
            ArrayList<Produto> produto = new ArrayList<>();
            Cursor cursor = banco.query("produto", new String[]{"id", "codigo", "quantidade"},
                    null, null, null, null, null);

            while (cursor.moveToNext()){

                Produto p = new Produto();
                p.setId(cursor.getInt(0));
                p.setCodigo(cursor.getString(1));
                p.setQuantidade(cursor.getString(2));
                produto.add(p);
            }
            return produto;
    }
}

I know it looks awful and everything I got was from internet tutorials, but any light is welcome thanks in advance!

the logcat


2020-03-18 21:51:36.374 29503-29503/? I/Timeline: Timeline: Activity_launch_request id:com.example.barcode time:26457815
2020-03-18 21:51:36.395 29503-29503/? D/Editor: hideClipTrayIfNeeded() TextView is focused!! hideClipTray()
2020-03-18 21:51:36.578 29503-29531/? D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
2020-03-18 21:51:36.603 29503-29503/? D/AndroidRuntime: Shutting down VM
2020-03-18 21:51:36.614 29503-29503/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.barcode, PID: 29503
    java.lang.ClassCastException: androidx.cardview.widget.CardView cannot be cast to android.widget.TextView
        at com.example.barcode.MyAdapter.onCreateViewHolder(MyAdapter.java:40)
        at com.example.barcode.MyAdapter.onCreateViewHolder(MyAdapter.java:15)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851)
        at androidx.recyclerview.widget.RecyclerView.onLayout(RecyclerView.java:4404)
        at android.view.View.layout(View.java:17745)
        at android.view.ViewGroup.layout(ViewGroup.java:5645)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1080)
        at android.view.View.layout(View.java:17745)
oussama-zaoui :

try this :

 public static class MyViewHolder extends RecyclerView.ViewHolder {
    // each data item is just a string in this case
    public TextView textView1;
    public TextView textView2;
    public TextView textView3;

    public MyViewHolder(View v) {
        super(v);
        textView1 = v.findViewById(R.id.textView);
        textView2 = v.findViewById(R.id.textView2);
        textView3 = v.findViewById(R.id.textView3);
    }
}

and :

public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    //this is where I get shit
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.itemlista, parent, false);

    MyViewHolder vh = new MyViewHolder(V);
    return vh;


}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=317241&siteId=1