2

I have a listview in my android program that gets its information from an ArrayList adapter. I have three methods that call listview.invalidateViews().

Two of these methods work without fail, and the third seems to freeze the listview. The information is correctly saved when backing out of the activity and on a screen rotate. But without taking these actions, the listview does not update.

Any Ideas?

UPDATE:

These instances work:

public void onItemClick(AdapterView<?> a, View v, int index, long id) {
    al.remove(index);
    adapter.notifyDataSetChanged();
}

public void addToList(View view) {
    EditText et = (EditText) findViewById(R.id.ListText1);
    if (et.getText().toString().equals("")) {
        //do nothing
    }
    else { 
        al.add(et.getText().toString());
        adapter.notifyDataSetChanged();
        et.setText(null);
    }
}

This method does not work:

public void resetList(View view) {
    al = new ArrayList<String>();
    adapter.notifyDataSetChanged();
}
plasticle
  • 23
  • 3

1 Answers1

0

you are using invalidateViews() differently, if you want to change the view of the listview's child then you can use the invalidateViews() but if you are changing the data/content of the listview's adapter you need to use notifyDataSetChanged() method.

the difference of the two are discussed in this question

ListView.invalidateViews()

 is used to tell the ListView to invalidate all its child item views (redraw them). Note that there not need to be an equal number of views than items. That's because a ListView recycles its item views and moves them around the screen in a smart way while you scroll.

Adapter.notifyDataSetChanged()

on the other hand, is to tell the observer of the adapter that the contents of what is being adapted have changed. Notifying the dataset changed will cause the listview to invoke your adapters methods again to adjust scrollbars, regenerate item views, etc...

and with your method

public void resetList(View view) {
    al = new ArrayList<String>();
    adapter.notifyDataSetChanged();
}

making a new object of ArrayList<String>(); will not reset the data of your list view. just because the al ArrayList that you passed on your adapter is now different to your al = new ArrayList<String>(); what you need to do now is to access your current arraylist then clearing its content with al.clear() method.

Community
  • 1
  • 1
Ker p pag
  • 1,568
  • 12
  • 25