java-`notifyDataSetChanged`我缺少什么?

我有一个活动,在视图上绘制了一些图标.

它在onCreate()中设置一个list和listAdapter.

我已将分配列表给我的适配器的代码提取到公共方法.

这样,外部代码就可以调用UI线程并为适配器分配新列表,并使其通过notifyDataSetChange()通知更改.

但是,不会绘制新图标,而是仅在离开并返回到活动之后绘制.

我怎样才能解决这个问题?

我试过adapter.clear()

并仔细检查了UI线程是否在运行此代码.

还有什么?

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

    SettingValueAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings_values);
        ((TitleBar)findViewById(R.id.theTitleBar)).init(this,DisplayStrings.DS_CATEGORIES);

        adapter = new SettingValueAdapter(this);

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

            @Override
            public void onComplete(Category[] aCategories) {
                categories = aCategories;
                refreshListIcons();
            }
        });
        final ListView list = (ListView)findViewById(R.id.settingsValueList);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            ..
        });
    }

    @Override
    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");
        }
        adapter.setValues(values);
    }   
}




public class SettingValueAdapter extends BaseAdapter {

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

...

    @Override
    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(R.id.itemText);
        ImageView iconView = (ImageView) convertView.findViewById(R.id.itemIcon);
        if (iconView != null && (item != null) && (item.icon != null)) {
            iconView.setImageDrawable(item.icon);
            iconView.setVisibility(View.VISIBLE);
        } else {
            iconView.setVisibility(View.GONE);
        }
        name.setText(item.display);
        name.setChecked(item.isSelected);
        View container = convertView.findViewById(R.id.itemContainer);
        if (position == 0) {
            ..
        return convertView;
    }
    public void setValues(SettingsValue[] values) {
        this.values = values;
        notifyDataSetChanged();
    }

}

解决方法:

您应该扩展ArrayAdapter而不是BaseAdapter:

http://developer.android.com/reference/android/widget/ArrayAdapter.html

此类适用于数组和集合,并且可以正确处理有关内部状态的所有问题.

在这种情况下,请在调用notifyDataSetChanged之前,先在适配器对象本身上使用clear,然后使用addAll.另一个方法是更改​​绑定到适配器的数组或集合.

我在这里找到了一个做得很好的教程:

http://www.vogella.com/articles/AndroidListView/article.html

希望能帮助到你 !!最好的祝福 !!

上一篇:我在项目中使用带有PHP的工作单位设计模式


下一篇:java-调用`invalidateViews()`后重新绘制项目时