-1

I am unable to create Shared Preference file I have been struggling with this for 2 days please help,I am new here.In my app I am having 15 question each on different screens and I want to store the text of the option selected so that I can use it in future. My Code

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

}

user3699550
  • 105
  • 2
  • 12
  • Shared prefrence is not a File this is a way to store text in internal memory if you want to use it some other activity then you can access it – Amitsharma Feb 06 '15 at 13:20
  • With file, I meant i cant find SP in the data folder – user3699550 Feb 06 '15 at 13:25
  • shared prefrence is not use for reading data or get data from file this is use to store valu in internal application only in side of variable which will always store if you clean it then memory will cleen else not – Amitsharma Feb 06 '15 at 13:29
  • @amitsharma Shared prefrences is wa way to store text for example between app lunches but its actually done by saving this data to a file. So Shared preferences is a file :) – Karol Żygłowicz Feb 06 '15 at 13:32
  • @ user user3699550 check correct if you satisfy with my answer answer 3rd – Amitsharma Mar 13 '15 at 08:03

2 Answers2

1

Try to change commit() into apply() something like this:

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
}

to read your prefs try:

 SharedPreferences prefs = this.getSharedPreferences("your.project.package.name", Context.MODE_PRIVATE);
 String idValue= prefs.getString("idValue", null); 
Karol Żygłowicz
  • 2,442
  • 2
  • 26
  • 35
  • /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml But only if you have rooted phone i believe you opened it using private mod. Try reading it like I sugested – Karol Żygłowicz Feb 06 '15 at 13:42
  • Yeah I am using private_Mode but nothing is there in data folder, but i guess it is not saving in the folder – user3699550 Feb 06 '15 at 13:48
  • I am getting int value of the last question – user3699550 Feb 06 '15 at 14:49
  • probably because you overwrite your variable mapped by string key "idValue". SharedPreferences it's not a list its hashMap. If you want to store another value you need to put another key like "idValue2" – Karol Żygłowicz Feb 06 '15 at 14:57
  • I have no idea how to proceed??It would be really appreciable if you could help me in this – user3699550 Feb 06 '15 at 15:00
  • You canto use Json standard to store your id's and using method toString() on JSONObject save it to sharedPreferences. Read about JSON and how to store and retrieve data with it:) – Karol Żygłowicz Feb 06 '15 at 15:16
0

Try deleting this line: SharedPreferences prefs = getActivity().getSharedPreferences( "idValue", 0 );

Admir Heric
  • 117
  • 1
  • 3
  • It won't help defining SharedPreferences prefs again jus covers the global variable and you are working on local one but that's not a problem – Karol Żygłowicz Feb 06 '15 at 13:37