Popular a Listview with Arrayadapter

Asked

Viewed 853 times

2

I need a popular ListView. I have a class that represents the Entity Cliete, a class that is responsible for the database database and finally a class ClienteDAO, that the data of the entity remains Cliente.

Follow the Classes:

Clientele

public class ClienteDAO {

private SQLiteDatabase DB;
    protected Banco auxBanco;
    private String TABELA = "CLIENTE";

    public ClienteDAO(Context context)
    {
            Banco newBanco = new Banco(context);
    DB = newBanco.getWritableDatabase();
    //newBanco.onCreate(DB);
    this.auxBanco = newBanco;
}

public ArrayList<Cliente> Listar_Clientes(){

    ArrayList<Cliente> list = new ArrayList<Cliente>();

    String[] colunas = new String[]{"_ID", 
                                    "NOME",
                                    "APELIDO",
                                    "OBSERVACAO",
                                    "CELULAR",
                                    "DATA_CADASTRO",
                                    "DDD"};

    Cursor Sn = DB.query(this.TABELA, colunas, null, null, null, null, "NOME ASC");

    if(Sn.getCount() > 0){
        Sn.moveToFirst();

        do{ 
            Cliente newCliente = new Cliente();
            newCliente.set_ID(Sn.getLong(Sn.getColumnIndex("_ID")));
            newCliente.setNome(Sn.getString(Sn.getColumnIndex("NOME")));
            newCliente.setApelido(Sn.getString(Sn.getColumnIndex("APELIDO")));
            newCliente.setCelular(Sn.getString(Sn.getColumnIndex("CELULAR")));
            newCliente.setDDD(Sn.getString(Sn.getColumnIndex("DDD")));
            newCliente.setObservacao(Sn.getString(Sn.getColumnIndex("OBSERVACAO")));
            newCliente.setCelular(Sn.getString(Sn.getColumnIndex("DATA_CADASTRO")));
            list.add(newCliente);

        }while(Sn.moveToNext());
    }
    return(list);
    }

Clienteadapter

public class ClienteAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Cliente> list;

    public ClienteAdapter(Context context, ArrayList<Cliente> list){
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        list.size();
        return 0;
    }

    @Override
    public Object getItem(int position) {
        list.get(position);
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Cliente cCliente = list.get(position);
        View layout;

        if(convertView == null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            layout  = inflater.inflate(R.layout.itens_relacao_cliente, null);
        }else
        {
            layout = convertView;
        }

        TextView Nome = (TextView) layout.findViewById(R.id.lblNome);
        Nome.setText(cCliente.getNome());

        TextView Apelido = (TextView) layout.findViewById(R.id.lblApelido);
        Apelido.setText(cCliente.getApelido());

        return layout;
        }
    }

Relacao_clients

public class Relacao_cliente extends Activity {

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

    try {
        ClienteDAO BD = new ClienteDAO(this);
        ArrayList<Cliente> Lista_Clientes =  BD.Listar_Clientes();

        ListView ListViewR_Cliente = (ListView)     findViewById(R.id.lvRelacao_Cliente);
        ListViewR_Cliente.setAdapter(new ClienteAdapter(this,  Lista_Clientes));

            } catch (Exception e) {
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
        }
    }
}
  • 1

    What is your question? At first the code does what it should, according to your question.

  • The code doesn’t do what it’s supposed to. I would like to know if something is being done wrong, if any object needs to have a specific treatment before it is passed to another class. In this call I realized (debugging) that it does not enter the public View getView(Clienteadapter). ListViewR_Cliente.setAdapter(new ClienteAdapter(this, Lista_Clientes));

1 answer

2


Now I understand the problem with your code, actually there is a small error in Adapter, forgot to return the actual data.

The class with the hits is:

public class ClienteAdapter extends BaseAdapter {

    private Context context;
    private ArrayList<Cliente> list;

    public ClienteAdapter(Context context, ArrayList<Cliente> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
        // Em vez de return 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
        // Em vez de return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Restante do codigo ok
    }
}

How did 0 return in getCount, the ListView thinks that the Adapter this emptiness, soon nothing is generated. I believe that with these hits your Adapter work.

Browser other questions tagged

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