【问题标题】:how to use shared preferences with radio group如何使用与广播组共享的首选项
【发布时间】:2014-12-03 00:13:47
【问题描述】:

我在 next 和 previous 中有两个按钮,问题 textview 和 Radio button 中的答案选项。我必须在单击下一步按钮时将单选按钮值保存在共享首选项中,当我调用上一个按钮时,它会从下一个按钮获取共享首选项值,并在我调用上一个按钮时将其设置为单选按钮。但是在运行应用程序单选按钮之后值保存在共享首选项中。但是当我调用上一个按钮时,没有获得共享首选项值,也没有显示选中单选按钮。有人可以在这里帮助我吗。谢谢高级。

这是我的活动代码。

 read_Questions_XML_File();

          button_Previouse.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    previousQuestionCalled(v);
                }
            });


            button_Next.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    nextQuestionCalled(v);
                }
            });


public void nextQuestionCalled(View view)
    {

        int id = Options_RadioGroup.getCheckedRadioButtonId();
        System.out.println("id = " + id);
        if (id > 0)
        {   

            Options_RadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(RadioGroup R_Group, int checked_ID) {
                    // TODO Auto-generated method stub

                    RadioButton radioButton = (RadioButton)R_Group. findViewById(checked_ID);
                    int checkedIndex = R_Group.indexOfChild(radioButton);
                    System.out.println("checkedIndex = " + checkedIndex);

                    SharedPreferences sharedPreferences = getSharedPreferences("RGROUP_SHARED_PREF", MODE_PRIVATE);
                    SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putInt("check", checkedIndex);
                    editor.commit(); 


                }
            });


            //if animationForward == false
            if (animationForward)
            {
                storingResult();
                boolean flag;
                if (animationForward)
                {  flag = false; }

                else 
                { flag = true; }

                animationForward = flag;
            }

        //Return The Number Of Element in this Vector > 0
        //-1 + 1
        if (-1 + Vectore_mquestionDatabaseStructure.size() > StaticClass.QuestionNumber)
        {
            if (StaticClass.isTest) 
            {
                StaticClass.resultOfTest_Out_OF = 1 + StaticClass.resultOfTest_Out_OF;
            }

            StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
            reHitting();
            view.clearAnimation();

            if (!StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            if (StaticClass.isTest) {
                button_QuestionLimit.clearAnimation();
            }

            return;
        }

        else
        {
            button_QuestionLimit.startAnimation(mAnimation);
            return;
        }
    }

        else if (id == -1)
        {
            Toast.makeText(this, "Please Select Any Option", Toast.LENGTH_LONG)
                    .show();
        }
    }

 public void previousQuestionCalled(View view)
    {
         System.out.println("_________________in Previous Question____________");
         SharedPreferences sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);
         int savedRadioIndex = sharedPreferences.getInt("check", 0);
         System.out.println("savedRadioIndex is "+savedRadioIndex);
         RadioButton savedCheckedRadioButton = (RadioButton)Options_RadioGroup.getChildAt(savedRadioIndex);
         savedCheckedRadioButton.setChecked(true);
         System.out.println("_________________in Previous Question____________");

        if (!animationForward)
        {
            boolean flag;

            if (animationForward) 
            {
                flag = false;
            }
            else
            {
                flag = true;
            }
            animationForward = flag;
        }
        if (StaticClass.QuestionNumber > 0)
        {
            if (!StaticClass.isTest)
            {
                StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
                reHitting();
                button_QuestionLimit.clearAnimation();
            }
            view.clearAnimation();


        } 
        else 
        {
            button_QuestionLimit.startAnimation(mAnimation);
        }

    }

当我按下下一步按钮时,将共享首选项值保存在 = int checkedIndex = R_Group.indexOfChild(radioButton);

但是当我在这里按上一个按钮时,我得到共享首选项值为零或-1,并且未选中单选按钮。

这是我的 Logcat 信息

10-08 16:47:54.007: I/System.out(459): id = 2131165205
10-08 16:47:54.007: I/System.out(459): strLimit = 2/12
10-08 16:47:54.007: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.016: I/System.out(459): checkedIndex = 2
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): ************************IN Next Button******************
10-08 16:47:54.086: I/System.out(459): checkedIndex = -1
10-08 16:47:54.096: I/System.out(459): ************************IN Next Button******************
10-08 16:48:06.756: I/System.out(459): _________________in Previous Question____________
10-08 16:48:06.756: I/System.out(459): savedRadioIndex is -1

之后我从 DDMS 检查了 xml 文件,它不存储值 n。它还存储 -1 值。这里 log cat 显示共享首选项值。

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<int name="check" value="-1" />
</map>

【问题讨论】:

  • 谁能帮帮我。
  • 有人能帮帮我吗!!!!!!

标签: java android


【解决方案1】:

在 RadioGroup 中,您必须在尝试保存以获取 RadioGroup 中当前选定的选项时创建 localInstance。我创建了最简单的示例和 您可以按如下所述实现:

private void saveRadioChoice(){
SharedPreferences mSharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);

SharedPreferences.Editor editor = mSharedPref.edit();

// Initialize Radiogroup while saving choices
RadioGroup localRadioGroup = (RadioGroup) findViewById(R.id.choices);
editor.putInt(my_choice_key, localRadioGroup.indexOfChild(findViewById(localRadioGroup.getCheckedRadioButtonId())));
editor.apply();
}

保存该值后,可以如下检索

private void retrieveChoices(){

SharedPreferences sharedPref = getSharedPreferences(MY_PREF_KEY,MODE_PRIVATE);
int i = sharedPref.getInt(my_choice_key,-1);
if( i >= 0){
((RadioButton) ((RadioGroup)findViewById(R.id.myChoiceViewRadio)).getChildAt(i)).setChecked(true);
}


}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 1970-01-01
    • 2014-08-17
    • 2011-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多