Listview does not display loaded BD data

Asked

Viewed 353 times

0

I have a query screen, where she should display the items already registered. It just doesn’t show. At first, I thought it was because the information wasn’t being recorded in the comic book, but they’re.
I just, I didn’t think why they weren’t loaded... I guess it’s something I didn’t do, but I don’t know what it is.

Conscliente.java

package br.sysandroid;

import java.util.List;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import br.sysandroid.dao.banco.BancoDAO; 
import br.sysandroid.model.Cliente;
import br.sysandroid.dao.ClienteAdapter;
import br.sysandroid.dao.ClienteDAO;

public class ConsCliente extends ListActivity {

private ClienteDAO clienteDAO;
List<Cliente> clientes;
ClienteAdapter adapter;
BancoDAO bancoDAO;
int posicao = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.conscliente);
    bancoDAO = new BancoDAO(this);

    Button btCadastro = (Button) findViewById(R.id.btCadastro);
    btCadastro.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            startActivity(new Intent(ConsCliente.this, CadCliente.class));
        }
    });
}

@Override
protected void onResume() {
    clienteDAO = new ClienteDAO(this);
    clienteDAO.open();
    clientes = clienteDAO.lerClientes();

    adapter = new ClienteAdapter(this, clientes);
    setListAdapter(adapter);
    super.onResume();
    }
}

conscliente.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin" >

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="291dp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btCadastro"
            android:text="Cadastro"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/btSair"
            android:text="Sair"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"  />

    </LinearLayout>
</LinearLayout>

Clienteadapter.java

package br.sysandroid.dao;

import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import android.widget.Toast;
import br.sysandroid.R.id;
import br.sysandroid.R.layout;
import br.sysandroid.model.Cliente;

public class ClienteAdapter extends BaseAdapter {

    private Context context;

    private List<Cliente> clientes;
    private LayoutInflater inflater;

    public ClienteAdapter(Context context, List<Cliente> clientes) {
        super();
        this.context = context;
        this.clientes = clientes;
        this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public void notifyDataSetChanged() {
        try {
            super.notifyDataSetChanged();
        } catch (Exception e) {
            trace("Erro: " + e.getMessage());
        }
    }

    private void trace(String msg) {
        toast(msg);
    }

    public void toast(String msg) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
    }

    public int getCount() {
        return 0;
    }

    public void remove(final Cliente cliente) {
        this.clientes.remove(cliente);
    }

    public void add(final Cliente clientes) {
        this.clientes.add((Cliente) clientes);
    }

    public Object getItem(int position) {
        return clientes.get(position);
    }

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

    public View getView(int position, View convertView, ViewGroup viewGroup2) {
        try {
            Cliente cliente = clientes.get(position);

            ViewHolder holder;

            if (convertView == null) {
                convertView = inflater.inflate(layout.linha_cliente, null);

                holder = new ViewHolder();
                holder.tvNome = (TextView) convertView.findViewById(id.tvRazaoSocialNome);
                holder.tvTelefone = (TextView) convertView.findViewById(id.tvTelefone);
                holder.tvEmail = (TextView) convertView.findViewById(id.tvEmail);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            holder.tvNome.setText(cliente.getNome());
            holder.tvTelefone.setText(cliente.getTelefone());
            holder.tvEmail.setText(cliente.getEmail());

            return convertView;
        } catch (Exception e) {
            trace("Erro: " + e.getMessage());
        }

        return convertView;
    }

    static class ViewHolder {
        public TextView tvNome;
        public TextView tvTelefone;
        public TextView tvEmail;
    }

}
  • What is this setListAdapter(adapter);?

  • @Fernando is a method of ListActivity, which is the specialisation of Activity that OP is using to display the list.

2 answers

2


I believe the problem is inside the Adapter.

Change the block,

public int getCount() {
    return 0;
}

for

public int getCount() {
    return clientes.size();
}
  • That’s right. Thanks man!

0

You must declare your ListView and add the adapter to it:

ListView tua_list_view = (ListView) findViewById(R.id.list);
tua_list_view.setListAdapter(adapter);

Browser other questions tagged

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