我无法创建共享首选项文件

时间:2015-02-06 13:15:59

标签: android android-fragments android-preferences android-radiogroup

我无法创建共享首选项文件我一直在努力奋斗2天请帮助,我是新来的。在我的应用程序中,我在不同的屏幕上有15个问题,我想存储选项的文本选择以便我将来可以使用它。 我的代码

公共类QuestionPagerFragment扩展了Fragment {

protected View mView;

String pageData[];  //Stores the text to swipe.
String optionData[];//Stores the option data.

static int rid;
RadioGroup group;
static ArrayList<Integer> list = new ArrayList();

SharedPreferences prefs;
public static final String MyPREFERENCES = "MyPrefs" ;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.page, container, false);
    return mView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    prefs = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    pageData=getResources().getStringArray(R.array.desserts);

    optionData=getResources().getStringArray(R.array.options);

    group = (RadioGroup)mView.findViewById(R.id.group);

    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            int radioButtonID = group.getCheckedRadioButtonId();
            View radioButton = group.findViewById(radioButtonID);
            rid = group.indexOfChild(radioButton);
            list.add(getArguments().getInt("pos"), rid);
            click();

        }
    });
   ((TextView)mView.findViewById(R.id.textMessage)).setText(pageData[getArguments().getInt("pos")]);
    if(getArguments().getInt("pos") == 14) {
        ((Button)mView.findViewById(R.id.submitbtn)).setVisibility(View.VISIBLE);
        ((Button)mView.findViewById(R.id.submitbtn)).setOnClickListener(new View.OnClickListener() {
               public void onClick(View v){

               }                   
            });
    }

    for(int i = 0; i < group.getChildCount(); i++){
        ((RadioButton) group.getChildAt(i)).setText(optionData[(getArguments().getInt("pos")*4)+i]);
    }      
}



public static void save() {
       if(rid==0){
           MainActivity.FLAG_A++;                  
       }
       else if(rid==1){
           MainActivity.FLAG_B++;    
       }
       else if(rid==2){
           MainActivity.FLAG_C++;    
       }
       else if(rid==3){
           MainActivity.FLAG_D++;    
       }            
}
public void click(){

       SharedPreferences prefs = getActivity().getSharedPreferences( "idValue", 0 );
       SharedPreferences.Editor editor = prefs.edit();
       editor.putString( "idValue", list.get(getArguments().getInt("pos")).toString());
       editor.commit();
}

}

3 个答案:

答案 0 :(得分:0)

此代码将帮助您存储值

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();

//将数据存储为KEY / VALUE对

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", "int value");        // Saving integer
editor.putFloat("key_name3", "float value");    // Saving float
editor.putLong("key_name4", "long value");      // Saving long
editor.putString("key_name5", "string value");  // Saving string

// Save the changes in SharedPreferences
editor.commit(); // commit changes

//获取SharedPreferences数据

//如果key的值不存在则返回第二个参数值 - 在这种情况下为null

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", null);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String

//从SharedPreferences中删除键值

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4

// Save the changes in SharedPreferences
editor.commit(); // commit changes

//清除SharedPreferences的所有数据

 editor.clear();
 editor.commit(); // commit changes

答案 1 :(得分:0)

尝试删除此行: SharedPreferences prefs = getActivity()。getSharedPreferences(&#34; idValue&#34;,0);

答案 2 :(得分:0)

尝试将commit()更改为apply(),如下所示:

public void click(){

   SharedPreferences prefs = getActivity().getSharedPreferences( "your.project.package.name", 0 ); // don't use short id because sharedPreferences is a global file
   SharedPreferences.Editor editor = prefs.edit();
   editor.putString( "idValue", list.get(getArguments().getInt("pos")).toString());

   editor.apply();  //Here is the change
}

阅读你的prefs试试:

 SharedPreferences prefs = this.getSharedPreferences("your.project.package.name", Context.MODE_PRIVATE);
 String idValue= prefs.getString("idValue", null);