首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >还原FragmentStatePagerAdapter中的片段错误还原

还原FragmentStatePagerAdapter中的片段错误还原
EN

Stack Overflow用户
提问于 2013-02-20 03:42:36
回答 1查看 444关注 0票数 0

我有一个测验应用程序,它将多项选择题(TextView,下面有5个RadioButtons选项)作为片段显示在ViewPager中的FragmentStatePagerAdapter中。

我得到的错误只有在片段恢复时才会出现。向前看,它完美地加载了每个问题和所有5个选项。但是,当它恢复时,加载完全错误,所有5个RadioButtons显示相同的文本,特别是最后一个RadioButton的文本。我不知道为什么会发生这个错误,但我认为这可能与调用超类有关,但这只是一种预感。

代码如下:

OnCreateView按照logcat确定的方式正确设置文本:

代码语言:javascript
复制
for (String s : aArray) {
// Runs through a string array of the 5 answers
        LinearLayout l = lArray.get(pos);

            RadioButton r = (RadioButton) l.findViewById(R.id.radio);
            Log.d(s, correctanswer);
//Log tag shows that the choices during initial creation or **restore** are the different 5 answers
            r.setText(s);
        }
    return rootView;

这是我的OnViewStateRestored

代码语言:javascript
复制
public void onViewStateRestored(Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);
    for (LinearLayout l : lArray) {
        RadioButton r = (RadioButton) l.findViewById(R.id.radio);
        Log.e(r.getText().toString(), correctanswer);
//Suddenly the displayed text is the same for all 5 buttons
        if (r.isChecked())
            r.performClick();
//android automatically saves check status. for full restore I click all the buttons
    }
}

如果您需要更多来自OnCreateView的代码或其他任何东西,请让我知道。我似乎找不出是什么导致了这个问题,并提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-20 03:50:37

你的每个单选按钮应该有不同的id,而不是它们都有相同的id。这就是单选按钮和其他视图通过id保存状态的方式。更准确地说,它们将其状态保存在由其id映射的SparseArray中。这就是为什么他们都有关于恢复的第五个的信息。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14966006

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档