samedi 23 avril 2016

Android Studio - ListView refresh isn't working after insert data in database

Basicly I use a method to return a list (with all the contacts in the database) that will ovewrite a list that is the base list to my listview. After that "overwriting" I try to refresh the list using ((BaseAdapter)EmergenciaFragment.lvContatosEmergencia.getAdapter()).notifyDataSetChanged(); but nothing happens. I only can refresh the listView finishing the activity and oppening it again.

I use this method to return a List with all the contacts in the database:

public List<Contato> buscaContatos(int tipo) {
    List<Contato> listContatos = new ArrayList<>();

    String table = Utility.getTableName(tipo);

    Cursor cursor = connection.query(table, null, null, null, null, null, null);

    Log.d("Log","List size: " + cursor.getCount());

    if (cursor.moveToFirst()) {

        cursor.moveToFirst();

        do {
            listContatos.add(new Contato(cursor.getString(1), cursor.getString(0), cursor.getString(2), tipo));
        } while(cursor.moveToNext());

    }

    cursor.close();
    return listContatos;
}

And I try to use this method to refresh de listView

public static void refreshListView() {

    EmergenciaFragment.contatos = MainActivity.dbController.buscaContatos(Contato.EMERGENCIA);
    ((BaseAdapter)EmergenciaFragment.lvContatosEmergencia.getAdapter()).notifyDataSetChanged();

}

This method is called on the activity that contains the fragment that contains the listview

@Override
public void onResume() {
    super.onResume();

    EmergenciaFragment.refreshListView();

}

The list EmergenciaFragment.contatos is the base list to the listView;

Aucun commentaire:

Enregistrer un commentaire