本文介绍了Android notifyDataSetChanged()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ArrayAdapter和ListView.我有一个活动 A 和一个活动 B .活动 A 启动活动 B (startActivityForResult()),并在活动 B 完成后将结果返回给活动 A .

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.

活动 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问题1
  2. Stackoverflow问题2
  3. Stackoverflow问题3
  4. 博客链接
  5. Stackoverflow问题4
  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.

推荐答案

尝试移动

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

在onCreate中的if语句之前

before if statement in onCreate

这篇关于Android notifyDataSetChanged()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 12:36