-1

我无法创建共享首选项文件我已经为此苦苦挣扎了 2 天,请帮忙,我是新来的。在我的应用程序中,我在不同的屏幕上有 15 个问题,我想存储所选选项的文本,以便我将来可以使用它。我的代码

公共类 QuestionPagerFragment 扩展片段 {

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

}

4

3 回答 3

1

尝试将 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
}

阅读您的偏好尝试:

 SharedPreferences prefs = this.getSharedPreferences("your.project.package.name", Context.MODE_PRIVATE);
 String idValue= prefs.getString("idValue", null); 
于 2015-02-06T13:28:05.933 回答
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 数据

// 如果键的值不存在,则返回第二个参数值 - 在这种情况下为 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 中删除 Key 值

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
于 2015-02-06T13:25:12.170 回答
0

尝试删除这一行: SharedPreferences prefs = getActivity().getSharedPreferences( "idValue", 0 );

于 2015-02-06T13:25:42.763 回答