0

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;

Hugo Sartori
  • 560
  • 6
  • 21
  • Your question doesn't make sense. Why do you need static method and static variable in here? You can do all work in your EmergenciaFragment. – sonngaytho Apr 24 '16 at 04:48
  • Why should I create a non-static method if it can be static? – Hugo Sartori Apr 24 '16 at 04:53
  • Static variables will take your memory. And more importantly, static method will not be able to override. It takes a lot of flexibility out from your design. You can find for refer with @tetsuo's answer in here: http://stackoverflow.com/questions/2671496/java-when-to-use-static-methods – sonngaytho Apr 24 '16 at 07:11
  • But did you understand my problem? I ovewrite the list of the listview with a method that return all the objects of the database and call notifyDataSetChanged(), but nothing happens. – Hugo Sartori Apr 24 '16 at 17:46
  • Static method can cause concurrency bugs and/or bottlenecks. Some problems are hard to find. Maybe you refresh the listview before a new list is updated. I need to debug to see what is your problem. – sonngaytho Apr 25 '16 at 01:44

0 Answers0