Java – ‘notifyDataSetChanged’ What am I missing?

‘notifyDataSetChanged’ What am I missing?… here is a solution to the problem.

‘notifyDataSetChanged’ What am I missing?

I have an activity that draws some icons on the View.

It sets a list and listAdapter in onCreate().

I’ve extracted the code used to assign the list to my adapter into a public method.

So that the external code can call the UI thread and assign the new list to the adapter, and have it notify of changes through notifyDataSetChange().

However, the new icon is not drawn and will only be drawn after leaving and returning to the activity.

How do I fix this?

I tried adapter.clear().

and double-checked that the UI thread was running this code.

What else?

public class CategoriesActivity extends ActivityBase {
    private Category[] categories;

SettingValueAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {

adapter = new SettingValueAdapter(this);

DriveToNativeManager nativeManager = DriveToNativeManager.getInstance();
        nativeManager.getCategories(new CategoriesListener() {

            public void onComplete(Category[] aCategories) {
                categories = aCategories;
        final ListView list = (ListView)findViewById(;
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

public void refreshListIcons()  {
        NativeManager nativeManager = AppService.getNativeManager();
        SettingsValue[] values = new SettingsValue[categories.length];
        for (int i = 0; i < categories.length; i++) {
            values[i] = new SettingsValue(categories[i].value, nativeManager.getLanguageString(categories[i].displayString), false);
            values[i].icon = ResManager.GetSkinDrawable(categories[i].iconName + ".bin");

public class SettingValueAdapter extends BaseAdapter {

private SettingsValue[] values;
    private LayoutInflater inflater;
    public SettingValueAdapter(Context context) {
        inflater = LayoutInflater.from(context);


    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.settings_item, null);
        SettingsValue item = values[position];
        CheckedTextView name = (CheckedTextView) convertView.findViewById(;
        ImageView iconView = (ImageView) convertView.findViewById(;
        if (iconView != null && (item != null) && (item.icon != null)) {
        } else {
        View container = convertView.findViewById(;
        if (position == 0) {
        return convertView;
    public void setValues(SettingsValue[] values) {
        this.values = values;



You should extend the ArrayAdapter instead of the BaseAdapter:

This class works well for arrays and collections, and it handles all questions about internal state correctly.

In this case, use clear followed by addAll in the Adapter object itself before calling notifyDataSetChanged. Another way is to change the binding to your adapter's Array or Collection.

I found a tutorial here that is well done :

Hope it helps!! Best regards!!

Related Problems and Solutions