共有プリファレンス ファイルを作成できません 2 日間この問題に取り組んできました。助けてください。私はここに来たばかりです。私のアプリでは、異なる画面にそれぞれ 15 の質問があり、選択したオプションのテキストを保存したいと考えています。今後も使えます。マイコード
public class QuestionPagerFragment extends 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();
}
}