我无法创建共享首选项文件我已经为此苦苦挣扎了 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();
}
}