如何使用无线电集团共享preferences [英] how to use shared preferences with radio group

查看:151
本文介绍了如何使用无线电集团共享preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在旁边和previous,TextView的问题和单选按钮他们的答案选项的两个按钮。我要节省点击下一步按钮,在共享preferences单选按钮值,当我打电话previous按钮,它会从下一键共享preferences值并将其设置为单选按钮时,我叫previous button.But后运行该应用程序单选按钮,价值共享preferences.But节省当我打电话previous按钮没有得到共享preferences价值并不显示单选按钮被选中。可有一个人帮我here.Thanks高级。

下面是我的活动code。

  read_Questions_XML_File();          button_ previouse.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根
                    previousQuestionCalled(V);
                }
            });
            button_Next.setOnClickListener(新OnClickListener(){                @覆盖
                公共无效的onClick(视图v){
                    // TODO自动生成方法存根                    nextQuestionCalled(五);
                }
            });
公共无效nextQuestionCalled(查看视图)
    {        INT ID = Options_RadioGroup.getCheckedRadioButtonId();
        的System.out.println(ID =+ ID);
        如果(ID大于0)
        {            Options_RadioGroup.setOnCheckedChangeListener(新OnCheckedChangeListener(){                @覆盖
                公共无效onCheckedChanged(RadioGroup中R_Group,诠释checked_ID){
                    // TODO自动生成方法存根                    单选按钮单选按钮=(单选)R_Group。 findViewById(checked_ID);
                    INT checkedIndex = R_Group.indexOfChild(单选)
                    的System.out.println(checkedIndex =+ checkedIndex);                    共享preferences共享preferences = getShared preferences(RGROUP_SHARED_ preF,MODE_PRIVATE);
                    共享preferences.Editor编辑=共享preferences.edit();
                    editor.putInt(检查,checkedIndex);
                    editor.commit();
                }
            });
            //如果animationForward ==假
            如果(animationForward)
            {
                storingResult();
                布尔标志;
                如果(animationForward)
                {标志= FALSE; }                其他
                {标志= TRUE; }                animationForward =标志;
            }        //返回此向量的制造&gt元素的数量; 0
        // - 1 + 1
        如果(1 + Vectore_mquestionDatabaseStructure.size()> StaticClass.QuestionNumber)
        {
            如果(StaticClass.isTest)
            {
                StaticClass.resultOfTest_Out_OF = ​​1 + StaticClass.resultOfTest_Out_OF;
            }            StaticClass.QuestionNumber = 1 + StaticClass.QuestionNumber;
            reHitting();
            view.clearAnimation();            如果(!StaticClass.isTest){
                button_QuestionLimit.clearAnimation();
            }            如果(StaticClass.isTest){
                button_QuestionLimit.clearAnimation();
            }            返回;
        }        其他
        {
            button_QuestionLimit.startAnimation(mAnimation);
            返回;
        }
    }        否则,如果(ID == -1)
        {
            Toast.makeText(这一点,请选择任意选项,Toast.LENGTH_LONG)
                    。显示();
        }
    } 公共无效previousQuestionCalled(查看视图)
    {
         的System.out.println(_________________在previous Question____________);
         共享preferences共享preferences = getShared preferences(MY_SHARED_ preF,MODE_PRIVATE);
         INT savedRadioIndex =共享preferences.getInt(检查,0);
         的System.out.println(savedRadioIndex为+ savedRadioIndex);
         单选savedCheckedRadioButton =(单选)Options_RadioGroup.getChildAt(savedRadioIndex);
         savedCheckedRadioButton.setChecked(真);
         的System.out.println(_________________在previous Question____________);        如果(!animationForward)
        {
            布尔标志;            如果(animationForward)
            {
                标志= FALSE;
            }
            其他
            {
                标志=真实的;
            }
            animationForward =标志;
        }
        如果(StaticClass.QuestionNumber大于0)
        {
            如果(!StaticClass.isTest)
            {
                StaticClass.QuestionNumber = -1 + StaticClass.QuestionNumber;
                reHitting();
                button_QuestionLimit.clearAnimation();
            }
            view.clearAnimation();
        }
        其他
        {
            button_QuestionLimit.startAnimation(mAnimation);
        }    }

当我preSS下一步按钮保存共享preFS在= INT checkedIndex = R_Group.indexOfChild值(单选);

但是,当我在这里preSS previous按钮,我得到共享preFS值为零或-1,单选按钮被选中。

下面是我的logcat信息

  10-08 16:47:54.007:我/的System.out(459):ID = 2131165205
10-08 16:47:54.007:我/的System.out(459):strLimit = 2/12
10-08 16:47:54.007:我/的System.out(459):**********在明年按钮***** *************
10-08 16:47:54.016:我/的System.out(459):checkedIndex = 2
10-08 16:47:54.086:我/的System.out(459):**********在明年按钮***** *************
10-08 16:47:54.086:我/的System.out(459):**********在明年按钮***** *************
10-08 16:47:54.086:我/的System.out(459):checkedIndex = -1
10-08 16:47:54.096:我/的System.out(459):**********在明年按钮***** *************
10-08 16:48:06.756:我/的System.out(459):_________________in previous Question____________
10-08 16:48:06.756:我/的System.out(459):savedRadioIndex是-1

之后,我已经检查XML文件从DDMS它不是存储值n表示。它还存储值-1。而在此登录猫显示的共享preFS值。

 < XML版本='1.0'编码='UTF-8'独立='是'&GT?;
<地图>
< INT NAME =检查值= - 1/>
< /图>


解决方案

在RadioGroup中,你必须创建localInstance,而你试图保存,以获得当前选择的选择在RadioGroup中。我已经简单的例子创建
可以实现提到如下:

 私人无效saveRadioChoice(){
共享preferences mShared preF = getShared preferences(MY_ preF_KEY,MODE_PRIVATE);共享preferences.Editor编辑= mShared pref.edit();//初始化RadioGroup中同时节省选择
RadioGroup中localRadioGroup =(RadioGroup中)findViewById(R.id.choices);
editor.putInt(my_choice_key,localRadioGroup.indexOfChild(findViewById(localRadioGroup.getCheckedRadioButtonId())));
editor.apply();
}

保存此值后,可以按以下方式进行检索。

 私人无效retrieveChoices(){共享preferences共享preF = getShared preferences(MY_ preF_KEY,MODE_PRIVATE);
INT I =共享pref.getInt(my_choice_key,-1);
如果(ⅰ> = 0){
(。(单选)((RadioGroup中)findViewById(R.id.myChoiceViewRadio))getChildAt(I))setChecked(真)。
}
}

I have two buttons in next and previous , question textview and their answers option in Radio button . I have to save the radio buttons values in Shared Preferences on click Next button and when i call previous button it gets the Shared Preferences value from next button and set it into radio button when i call the previous button.But after run the application radio button value saves in shared preferences.But When i call previous button does not get the Shared Preferences value and does not show the Radio button is checked. Can some one help me here.Thanks in Advanced.

Here is my Activity code.

 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);
        }

    }

When i press Next button saved the Shared Prefs Value in = int checkedIndex = R_Group.indexOfChild(radioButton);

But when i press Previous button here I'm getting Shared Prefs Value is zero or -1 and Radio button is unchecked.

Here is my Logcat info

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

After that i have checked xml file from DDMS its not store the value n that .It also store the -1 value .And here log cat shows the shared prefs value.

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

解决方案

In RadioGroup, you have to create localInstance while you are trying to save to get the current selected choice in RadioGroup. I have created simplest example and You can implement as mentioned below :

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();
}

after saving this value, you can retrieve it as follows

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);
}


}

这篇关于如何使用无线电集团共享preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆