Sqlite data not available in Recyclerview

Asked

Viewed 218 times

1

I’m trying to list data in a Recyclerview, but in the 3 lines nothing appears and I don’t know what’s wrong, take a look at the image below:

inserir a descrição da imagem aqui

Adapter client.java

import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;

public class AdapterCliente extends RecyclerView.Adapter<AdapterCliente.MyViewHolder> {
private List<Cliente> listacliente;
private LayoutInflater mLayoutInflater;

public AdapterCliente(Context ctx, List<Cliente> cli){
    listacliente=cli;
    mLayoutInflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.i("LOG","onCreateViewHolder()");
    View v=mLayoutInflater.inflate(R.layout.item_cliente, parent, false);
    MyViewHolder mvh = new MyViewHolder(v);
    return mvh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Log.i("LOG","onBindViewHolder()");
    holder.tvNome.setText(listacliente.get(position).getNom_cli());
}

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

public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView tvNome;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvNome=(TextView) itemView.findViewById(R.id.textViewNom);
        }
    }
}

Clientefragment.Java

import android.support.v7.widget.RecyclerView;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import java.util.List;

    public class AdapterCliente extends RecyclerView.Adapter<AdapterCliente.MyViewHolder> {
private List<Cliente> listacliente;
private LayoutInflater mLayoutInflater;

public AdapterCliente(Context ctx, List<Cliente> cli){
    listacliente=cli;
    mLayoutInflater=(LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Log.i("LOG","onCreateViewHolder()");
    View v=mLayoutInflater.inflate(R.layout.item_cliente, parent, false);
    MyViewHolder mvh = new MyViewHolder(v);
    return mvh;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
    Log.i("LOG","onBindViewHolder()");
    holder.tvNome.setText(listacliente.get(position).getNom_cli());
}

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

public class MyViewHolder extends RecyclerView.ViewHolder{
    public TextView tvNome;
        public MyViewHolder(View itemView) {
            super(itemView);
            tvNome=(TextView) itemView.findViewById(R.id.textViewNom);
        }
    }
}

Java bank.

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;

public class Banco {
private Context ctx;
private String NOME_BANCO ="farda";
private String NOME_TABELA="cliente";
private String NOME_TABELA1="produto";
private SQLiteDatabase BancoDados= null;

public Banco(Context ctx){ this.ctx=ctx;}

public void CriaBancoCliente(){
    try {
        BancoDados = ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
        String sql = "CREATE TABLE IF NOT EXISTS " + NOME_TABELA + " (cod_cli INTEGER PRIMARY KEY,nom_cli TEXT,ende TEXT,tel TEXT)";
        BancoDados.execSQL(sql);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (BancoDados != null) {
            BancoDados.close();
        }
    }
}

public List<Cliente> listacliente(){
    ArrayList<Cliente> lista= new ArrayList<Cliente>() ;
    try {
        CriaBancoCliente();
        BancoDados=ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
        Cursor cursorCliente=BancoDados.rawQuery("SELECT cod_cli,nom_cli,ende,tel FROM " + NOME_TABELA + " ORDER BY nom_cli",null);
        if(cursorCliente!=null && cursorCliente.getCount()!=0){
            cursorCliente.moveToFirst();
            do {
                Cliente cliente= new Cliente();
                cliente.setCod_cli(cursorCliente.getString(cursorCliente.getColumnIndex("nom_cli")));
                lista.add(cliente);
            } while (cursorCliente.moveToNext());
        }
        if (cursorCliente != null) {
            cursorCliente.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
        if (BancoDados != null) {
            BancoDados.close();
        }
    }
    return lista;
}
public int inserirCliente(Cliente cliente) {
    int retorno = 0;
    int cod=geracod();
    try {
        CriaBancoCliente();
        BancoDados = ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
        String sql = "INSERT INTO " + NOME_TABELA + " (cod_cli,nom_cli,tel,ende) VALUES ('"+ cod + "','"+ cliente.getNom_cli() + "','" + cliente.getTel() + "','" + cliente.getEnde() + "')";
        BancoDados.execSQL(sql);
    } catch (Exception e) {
        e.printStackTrace();
        retorno = 1;
    } finally {
        if (BancoDados != null) {
            BancoDados.close();
        }
    }
    return retorno;
}
public int geracod(){
    int codigo= 0;
    try {
        CriaBancoCliente();
        BancoDados=ctx.openOrCreateDatabase(NOME_BANCO, Context.MODE_PRIVATE, null);
        Cursor cursorCliente=BancoDados.rawQuery("SELECT MAX(cod_cli) as codigo FROM " + NOME_TABELA + "",null);
        if(cursorCliente!=null && cursorCliente.getCount()!=0){
            cursorCliente.moveToFirst();
            do {
                codigo=(cursorCliente.getInt(cursorCliente.getColumnIndex("codigo")));
            } while (cursorCliente.moveToNext());
        }
        if (cursorCliente != null) {
            cursorCliente.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally{
          if (BancoDados != null) {
              BancoDados.close();
          }
        }
        return codigo+1;

    }
}

item_client.xml

<?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"
android:paddingTop="20dp"
android:paddingLeft="16dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textViewNom"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

<View
    android:id="@+id/vw_divider"
    android:layout_width="match_parent"
    android:layout_height="0.8dp"
    android:background="@color/colorPrimary"
    android:layout_marginTop="10dp"
    android:layout_below="@+id/textViewNom"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

I’m done here, and I’m grateful to all of you who have the patience to help.

1 answer

0


I managed to find the error was in the Bank

 Cliente cliente= new Cliente();
            cliente.setCod_cli(cursorCliente.getString(cursorCliente.getColumnIndex("nom_cli")));
            lista.add(cliente);// No lugar de cliente.setCod_cli é cliente.setNom_cli

Browser other questions tagged

You are not signed in. Login or sign up in order to post.