0

hi i have tried to implement a shared preference and everything i try doesn't seem to work. i'm wanting when someone clicks on an item in a list view it stores data about that selection in a shared prerference. when the app loads up i want it to check for a shared preference ie: if there are no preferences go to this activity else go to another activity. i also would like to know if there are some preferences when i get sent to another activity how to retrieve them. its not the first time i have asked this question but i thought id re ask as i wasn't very clear

heres what i have tried so far

protected void onListItemClick (ListView l, View v, int position, long id) { 
        Intent intent = new Intent(this, HomeActivity.class);
        try {
            Log.v("lc", "try1");
            JSONObject singleTeamDictionary = teamNamesArray.getJSONObject(position);

            Log.v("lc", "dictionary:" + singleTeamDictionary);

            Log.v("lc", "try2");
             ChosenTeam = (String) singleTeamDictionary.get("name");
             ChosenTeamId = (String) singleTeamDictionary.get("team_id");
             ChosenLeagueId = (String) singleTeamDictionary.get("league_id");
             ChosenDivisionID = (String) singleTeamDictionary.get("division_id");

             Log.v("lc", "try3");

             Log.v("lc", "ChosenTeam: " + ChosenTeam);
             Log.v("lc", "ChosenTeamId: " + ChosenTeamId);
             Log.v("lc", "ChosenLeagueId: " + ChosenLeagueId);
             Log.v("lc", "ChosenDivisionID: " + ChosenDivisionID);          


        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Log.v("lc", "catch");

        }

        String curPos = Integer.toString(position);
    Log.v("lc", "teamPos: " + curPos);

    SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString("ChosenMethod", "Team");
    editor.putString("ChosenTeam", ChosenTeam);
    editor.putString("ChosenTeamId", ChosenTeamId);
    editor.putString("ChosenLeagueId", ChosenLeagueId);
    editor.putString("ChosenDivisionID", ChosenDivisionID);
    editor.commit();


        //or just use the position:
        intent.putExtra("itemIndex", curPos);
        intent.putExtra("fullData", fulldata); //or just the part you want
        startActivity(intent);
    }

then in the intro activity

public void checkPreferences(){


    SharedPreferences preferences = getSharedPreferences("prefs", Context.MODE_PRIVATE);

    String ChosenMethod = preferences.getString("chosenTeam", ChosenTeam);


    //String ChosenMethod = preferences.getString("ChosenMethod", null);
//getPWDFromSP()


    //SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
     //Log.v("myapp", "prefs = " + preferences);
     //String ChosenMethod = preferences.getString("ChosenMethod", chosenMethod);
     Log.v("myapp", "ChosenMethod = " + ChosenMethod);

     if (ChosenMethod != null){

         Intent intent = new Intent(TheEvoStikLeagueActivity.this,Activity.class);

     }




}


            @Override

            public void onCreate(Bundle icicle) {

                    super.onCreate(icicle);

                    setContentView(R.layout.main);
                    checkPreferences();
iamlukeyb
  • 6,487
  • 12
  • 29
  • 40

2 Answers2

0

"chosenTeam" is not the same as "ChosenTeam".

Rajesh
  • 15,724
  • 7
  • 46
  • 95
0

hope this code helps you

private SharedPreferences sp;
private Editor e;

//create methods
//save method(string)
private void savePreferences(String key,String val){
    sp = getSharedPreferences("prefs", 0);
    e = sp.edit();
    e.putString(key , val);
    e.commit();
}

//get method
private String getPreferences(String key){
    sp = getSharedPreferences("prefs", 0);
    String value = sp.getString(key, "");
    return value;
}

//save String
savePreferences("yourKey","yourString");

//get Preferences
getPreferences("yourKey")

//check value saved or not
if(getPreferences("yourKey").equals(null)){
   //do something
   Log.i("value","null");
}
else{
   //do something
   Log.i("value","exists");
}

if you want to save integers or boolean,you can edit methods and let return values as you wish.

Hassy31
  • 2,793
  • 3
  • 21
  • 37