Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
android共享偏好实现_Android_Listview_Preferences - Fatal编程技术网

android共享偏好实现

android共享偏好实现,android,listview,preferences,Android,Listview,Preferences,嗨,我尝试实现一个共享偏好,但我尝试的一切似乎都不起作用。我希望当有人在列表视图中单击某个项目时,它会将有关该选择的数据存储在共享的prerference中。当应用程序加载时,我希望它检查共享首选项,即:如果没有首选项,则转到此活动,否则转到其他活动。我还想知道,当我被发送到另一个活动时,是否有一些首选项以及如何检索它们。这不是我第一次问这个问题,但我想我会再问一次,因为我不是很清楚 这是我到目前为止试过的 protected void onListItemClick (ListView l,

嗨,我尝试实现一个共享偏好,但我尝试的一切似乎都不起作用。我希望当有人在列表视图中单击某个项目时,它会将有关该选择的数据存储在共享的prerference中。当应用程序加载时,我希望它检查共享首选项,即:如果没有首选项,则转到此活动,否则转到其他活动。我还想知道,当我被发送到另一个活动时,是否有一些首选项以及如何检索它们。这不是我第一次问这个问题,但我想我会再问一次,因为我不是很清楚

这是我到目前为止试过的

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

“chosenTeam”与“chosenTeam”不同。

希望此代码对您有所帮助

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");
}
如果要保存整数或布尔值,可以编辑方法 并让返回值如您所愿