Sqlite Data Does Not Appear in RecyclerView

1

I'm trying to list data in a RecyclerView, but on the 3 lines nothing appears and I do not know what's wrong, so look at the image below:

AdapterClient.java

importandroid.support.v7.widget.RecyclerView;importandroid.content.Context;importandroid.util.Log;importandroid.view.LayoutInflater;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.TextView;importjava.util.List;publicclassAdapterClienteextendsRecyclerView.Adapter<AdapterCliente.MyViewHolder>{privateList<Cliente>listacliente;privateLayoutInflatermLayoutInflater;publicAdapterCliente(Contextctx,List<Cliente>cli){listacliente=cli;mLayoutInflater=(LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);}@OverridepublicMyViewHolderonCreateViewHolder(ViewGroupparent,intviewType){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);
        }
    }
}

ClientFragment.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);
        }
    }
}

Banco.java

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 here, and thanks to all of you who have the patience to help.

    
asked by anonymous 08.01.2016 / 05:53

1 answer

0

I could 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
    
09.01.2016 / 23:21