Android notifyDataSetChanged() not working

会有一股神秘感。 提交于 2019-12-23 06:09:50

问题


I am working on ArrayAdapter and ListView. I have an Activity A and Activity B. Activity A starts an Activity B (startActivityForResult()) and when Activity B is finished, it returns the result back to Activity A.

Activity A:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    listViewPrayerList = (ListView) findViewById(R.id.listView_prayerlist);
    prayerList = new ArrayList<PrayerSetting>();
    if (getIntent().hasExtra(Utils.PRAYER_LIST)) {
        prayerList.clear();
        prayerList.addAll((ArrayList<PrayerSetting>) getIntent()
                .getSerializableExtra(Utils.PRAYER_LIST));
        pAdapter = new PrayerAdapter(this, R.layout.prayer_list_item,
                prayerList);
  }
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (data != null && data.hasExtra(Utils.SETTINGS)) {
        PrayerSetting p = (PrayerSetting) data
                .getSerializableExtra(Utils.SETTINGS);
        prayerList.set(itemClickedPosition, p);
        pAdapter.notifyDataSetChanged(); // UI/List doesn't change
    }
}

I don't understand what I am doing wrong! I have tried many solutions but still don't get it.

  1. Stackoverflow Question 1
  2. Stackoverflow Question 2
  3. Stackoverflow Question 3
  4. Blogspot link
  5. Stackoverflow Question 4

I do understand that its a basic question. Thanks in Advance.


回答1:


Try moving

pAdapter = new PrayerAdapter(this, R.layout.prayer_list_item,
    prayerList);

before if statement in onCreate



来源:https://stackoverflow.com/questions/23911398/android-notifydatasetchanged-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!