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