문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top