1. Are you ready for the Galaxy S20? Here is everything we know so far!

what am I missing with `notifyDataSetChanged`?

Discussion in 'Android Development' started by elad2109, Jun 27, 2013.

  1. elad2109

    elad2109 Lurker
    Thread Starter

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

    It sets a list and listAdapter in `onCreate()`.


    I have extracted to public method the code that assign the list to my adapter.

    So that external code, can call the UI thread and assign new list to the adapter and make it notify the change via `notifyDataSetChange()`

    However the new icons are not drawn, but only after leaving and getting back to the Activity.

    How can I fix this?

    I have tried adapter.clear()

    and doubled checked the UI thread runs this code.

    what else?



    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 = new SettingsValue(categories.value, nativeManager.getLanguageString(categories.displayString), false);
    values.icon = ResManager.GetSkinDrawable(categories.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) {
    if (position == values.length-1) {
    container.setBackgroundResource(R.drawable.item_selector_single);
    } else {
    container.setBackgroundResource(R.drawable.item_selector_top);
    }
    } else {
    if (position == values.length-1) {
    container.setBackgroundResource(R.drawable.item_selector_bottom);
    } else {
    container.setBackgroundResource(R.drawable.item_selector_middle);
    }
    }
    container.setPadding(0, 0, 0, 0);
    return convertView;
    }
    public void setValues(SettingsValue[] values) {
    this.values = values;
    notifyDataSetChanged();
    }

    }
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...

Share This Page

Loading...