Question

I've seen many examples / tutorials that explain how you can update an ArrayList used by a BaseAdapter. For example:

public class CustomAdapter extends BaseAdapter {
    private ArrayList<Item> listData;
    private LayoutInflater layoutInflater;

    public CustomListAdapter(Context context, ArrayList<Item> listData) {
            this.listData = listData;
            layoutInflater = LayoutInflater.from(context);
    }

    //called to update the ListView
    public void resetList(ArrayList<Item> newList) {
        this.listData = newList;
        this.notifyDataSetChanged();
    }

}

Another way is to clear and fill the original ArrayList where listData is currently pointing to and to call notifyDataSetChanged() from the Activity's thread.

Why is this an often recommended way? What happens if I click on a ListView item directly after or while the ArrayList is being modified but before notifyDataSetChanged() is called?

Was it helpful?

Solution

Since you are calling resetList() from Activity's (UI) thread, you cannot click an item in the middle of method ecexution, as clicks are processed by the same UI thread, therefore, you are able to perform a click only before or after the method execution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top