2012-05-09 5 views
0

안녕하세요, 공유 환경 설정을 구현하려했는데 시도한 모든 것이 작동하지 않는 것 같습니다. 나는 누군가가 목록보기에서 항목을 클릭하면 공유 prereference에 그 선택에 관한 데이터를 저장하고 싶다. 앱이로드되면 공유 환경 설정을 확인합니다. 즉, 환경 설정이없는 경우이 활동으로 이동하고 다른 활동으로 이동합니다. 나는 또한 내가 다른 활동으로 그들을 검색하는 방법을 보냈을 때 몇 가지 기본 설정이 있는지 알고 싶다. 이 아니 처음 내가이 질문을했지만 나는 매우 명확하지으로 ID를 다시 물어 생각android 공유 환경 설정 구현

을 heres 내가 지금까지 소개 활동 후

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

을 시도 것을

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

답변

0

"selectedTeam"은 "ChosenTeam"과 동일하지 않습니다.

0

희망이 코드는 정수 또는 부울을 저장하려는 경우, 당신은 방법 을 편집 할 수 있습니다 당신이 원하는대로 반환 값을 수 있도록 당신에게

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

을하는 데 도움이됩니다.