Android:notifyDataSetChanged()在Fragment中不起作用

时间:2017-01-04 10:32:57

标签: android android-fragments

这是Fragment

中代码的一部分
private AlarmOptionItemListAdapter listAdapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_create_new_alarm, null);

    listAdapter = new AlarmOptionItemListAdapter();
    ListView lvAlarmOptionList = (ListView) view.findViewById(R.id.lvAlarmOptionList);
    lvAlarmOptionList.setAdapter(listAdapter);
    initializeListItems();

    ...

    return view;
}

private void initializeListItems() {
    itemAlarmLock = new TextOnlyItem();
    itemAlarmLock.setTitle(getString(R.string.alarm_lock));
    itemAlarmLock.setSubtitle(getString(R.string.choose_option));
    listAdapter.addItem(itemAlarmLock);
    listAdapter.notifyDataSetChanged();
}

现在,我覆盖了此onActivityResult()中的Fragment方法。从这段代码中我计划更改ListView项目中的文字。

onActivityResult()从其他Fragment获取数据。使用带来的数据,我尝试用新的文本替换ListView项目中的文本。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == RESULT_OK && data != null) {
        switch(requestCode) {
            case CODE_PIN:
                String value = data.getStringExtra(Keys.VALUE);
                if(value != null) {
                    Log.i(ApplicationManager.LOG, "PIN set to " + value);
                    itemAlarmLock.setSubtitle(getString(R.string.pin));
                    listAdapter.notifyDataSetChanged();
                }
                break;

            case CODE_PASSWORD:
                String password = data.getStringExtra(Keys.VALUE);
                if(password != null) {
                    Log.i(ApplicationManager.LOG, "Password set to " + password);
                    itemAlarmLock.setSubtitle(getString(R.string.password));
                    listAdapter.notifyDataSetChanged();
                }
                break;

            case CODE_PATTERN:
                String pattern = data.getStringExtra(Keys.VALUE);
                if(pattern != null) {
                    Log.i(ApplicationManager.LOG, "Pattern set to " + pattern);
                    itemAlarmLock.setSubtitle(getString(R.string.pattern));
                    listAdapter.notifyDataSetChanged();
                }
                break;
        }
    }
}

当我运行此代码时,我发现Log正确显示,但不知何故列表视图不会更改该值。

我应该修改什么才能使其有效?

2 个答案:

答案 0 :(得分:0)

我认为以下代码可以解决您的问题:

在你调用startActivityForResult()的FragmentB中,写如下:

     getActivity().startActivityForResult(intent,MainActivity.CODE_PIN);

现在,在Activity的startActivityForResult()

     @Override
   public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK && data != null) {
    switch(requestCode) {
        case CODE_PIN:
            String value = data.getStringExtra(Keys.VALUE);
            if(value != null) {
            Fragment reference = null;
            List<Fragment> fragmentList =      getSupportFragmentManager().getFragments();
            for (int i = 0; i < fragmentList.size(); i++) {
                reference = fragmentList.get(i);
                if (reference != null && reference instanceof FragmentA) {
                    ((FragmentA) reference).refresh();
                }
            }
            }
            break;
          // Same for others REQUEST_CODE's
              }
}
}

在FragmentA中,按如下方式刷新列表:

   public void refresh()
 {
        itemAlarmLock.setSubtitle(getString(R.string.pin));
                listAdapter.notifyDataSetChanged();
   }

答案 1 :(得分:0)

您是否在strings.xml中声明了一个名为“pin”,“password”和“pattern”的String ressource?

也许我理解你的代码错了,但似乎你在尝试这个:

R.string.<value of variable>(“pin”,“密码”,“模式”在你的情况下) 这不行。 相反,你只需要这样做:

String variable = // your value
...
itemAlarmLock.setSubtitle(variable);

我希望我能正确理解您的代码,这将有助于