Popular ListView with ArrayAdapter

2

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

Follow the Classes:

ODD Client

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

ClientAdapter

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;
        }
    }

Customer_Relations

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);
        }
    }
}
    
asked by anonymous 12.06.2014 / 01:41

1 answer

2

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

The class with the correct answers 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
    }
}

As returned 0 in getCount , ListView thinks Adapter is empty, therefore nothing is generated. I believe that with these hits your Adapter works.

    
12.06.2014 / 02:50