jeudi 29 janvier 2015

Custom listview with empty arraylist

Listview with image and two textviews. It crashes the first time i enter if theres no object to print or to show. It cant get a blank listview if the arraylist is empty.



public contactoAdapter(Context context, ArrayList<contactoAgenda> datos) {
super(context, R.layout.listview_item, datos);
// Guardamos los par�metros en variables de clase.
this.context = context;
this.datos = datos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View item = convertView;
contactoHolder holder;

if (item == null) {
item = LayoutInflater.from(context).inflate(R.layout.listview_item,
null);

holder = new contactoHolder();
holder.imgContacto = (ImageView) item.findViewById(R.id.imagenContacto);
holder.tvMail = (TextView) item.findViewById(R.id.tvMail);
holder.tvNombre = (TextView) item.findViewById(R.id.tvNombre);

// Almacenamos el holder en el Tag de la vista.
item.setTag(holder);
}

holder = (contactoHolder) item.getTag();

holder.imgContacto.setImageResource(datos.get(position).getDrawableImageID());

holder.tvNombre.setText(datos.get(position).getNombre());
holder.tvMail.setText(datos.get(position).getDireccion());


return item;
}


I fill the arraylist with all the contacts in one table. So the first time the table is empty till you fill in with some contacts.



public ArrayList<contactoAgenda> recuperarTodosContactos() {
SQLiteDatabase db = getReadableDatabase();
ArrayList<contactoAgenda> lista_contactos = new ArrayList<contactoAgenda>();
String[] valores_recuperar = {"nombre","direccion","telefono","email","miembrofacebook","miembrotwitter","miembrogoogle","miembrolinkedin","sexo","tipocontacto","imagen"};
Cursor c = db.query("contactos", valores_recuperar, null, null, null, null, null, null);
c.moveToFirst();
do {
contactoAgenda contactos = new contactoAgenda(c.getString(0), c.getString(1), c.getString(2), c.getString(3),c.getInt(4), c.getInt(5), c.getInt(6), c.getInt(7),c.getInt(8), c.getString(9), c.getInt(10));
lista_contactos.add(contactos);
} while (c.moveToNext());


db.close();
c.close();
return lista_contactos;
}


I call it like using global variables, the important thing i get an arraylist of contacts to display in thee adapter. Every case works fine except the particular one. The first time the arraylist is empty. Ive tried a listview with contacts and delete them and i can get the listview in blank... Dont know the reason. If the arraylist is empty, it had to show a blank listview:



AgendaGlobal.getInstance().miAgenda = BaseDatosGlobal.getInstance(this).agendaBaseDatos.recuperarTodosContactos();

Toast.makeText(getApplicationContext(),"Hasta aqui ya no", Toast.LENGTH_LONG).show();

adapter = new contactoAdapter(this, AgendaGlobal.getInstance().miAgenda);

lvContactos = (ListView) findViewById(R.id.lvItems);
// Asignamos el Adapter al ListView, en este punto hacemos que el
// ListView muestre los datos que queremos.


lvContactos.setAdapter(adapter);

Aucun commentaire:

Enregistrer un commentaire