Android 在设置活动中更改文本后,它不会得到反映

Android 在设置活动中更改文本后,它不会得到反映,android,android-studio,Android,Android Studio,我在Udacity的教程(天气应用程序)的帮助下制作了我的第一个android应用程序 对于设置活动,我有一个文本框,其中保存位置值 例如,德克萨斯州。 但当我将其更改为任何其他值时,它不会在应用程序中反映相同的值。但是,该文本框中的值正在更改 在应用程序的设置活动中,我有以下代码 addPreferencesFromResource(R.xml.pref_general); bindPreferenceSummaryToValue(findPreference(getString(R.stri

我在Udacity的教程(天气应用程序)的帮助下制作了我的第一个android应用程序

对于设置活动,我有一个文本框,其中保存位置值

例如,德克萨斯州。 但当我将其更改为任何其他值时,它不会在应用程序中反映相同的值。但是,该文本框中的值正在更改

在应用程序的设置活动中,我有以下代码

addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_location_key)));
但到今天为止,
addPreferencesFromResource
已被弃用,因此我不确定如何在这方面继续。我用什么来改变它

我的目标是更改位置文本中的值,当我继续我的主要活动时,天气数据应该反映更改的位置数据

谢谢

编辑:


以上是设置活动的代码。但正如我所说,
addPreferencesFromResource
已被弃用。因此,这部分代码实际上不起作用。

创建一个单独的类,如下所示,以处理所有与prefs相关的操作、存储和恢复

public class MyPrefs {
Context context;
SharedPreferences systemPrefs;
SharedPreferences.Editor editor;

static MyPrefs prefs;

public static final String DEFAULT_LOCATION = "DEFAULT_LOCATION";
public static final String SOME_DEFAULT_LOCATION = "London";

public static MyPrefs getInstance(Context context){
    if(prefs == null){
        prefs = new MyPrefs(context);
    }
    return prefs;
}

private MyPrefs(Context context) {
    this.context = context;
    this.systemPrefs = context.getSharedPreferences(Constants.USER_PREFS, Activity.MODE_PRIVATE);
    this.editor = systemPrefs.edit();
}

public String getDefaultLocation(){
    return systemPrefs.getString(DEFAULT_LOCATION, SOME_DEFAULT_LOCATION);
}

public void setDefaultLocation(String newLocation){
    editor.putString(DEFAULT_LOCATION, newLocation);
    editor.commit();
}

public void clear() {
    editor.clear();
    editor.commit();
}
}
在任何活动课上都要用到这个

        MyPrefs myPrefs = MyPrefs.getInstance(this);
    //To get the location stored in prefs
    String locationFromPrefs = myPrefs.getDefaultLocation();

    //To store the location in prefs
    myPrefs.setDefaultLocation("New York");

你能发布更多的代码吗?private void updatewather(){FetchWeatherTask weatherTask=new FetchWeatherTask();SharedReferences prefs=PreferenceManager.GetDefaultSharedReferences(getActivity());String location=prefs.getString(R.String.pref\u location\u key)、getString(R.String.pref\u location\u default));weatherTask.execute(location);}----------------------这是updatewather函数。我需要更改pref_location_键。@floftking9987用格式发布问题是的,当然。我正在发布。谢谢。这对我没用。我在settingActivity中添加了第一个代码段,然后使用第二个代码段获取值,但它不起作用。谢谢。有什么问题吗?你没有价值?你调试了吗?是的,我调试了。问题是,在文本框中,我正在写入位置,但在主活动中,我正在获取我硬编码的默认位置的数据。我的主要问题是,在设置为addPreferencesFromResource不工作后,如何保存更改。我搜索的每一个地方,这个关键字总是在那里,但现在它不起作用。可能你在某个地方再次用默认值覆盖更新的值…等等,我会在几分钟内更新我的代码
public class MyPrefs {
Context context;
SharedPreferences systemPrefs;
SharedPreferences.Editor editor;

static MyPrefs prefs;

public static final String DEFAULT_LOCATION = "DEFAULT_LOCATION";
public static final String SOME_DEFAULT_LOCATION = "London";

public static MyPrefs getInstance(Context context){
    if(prefs == null){
        prefs = new MyPrefs(context);
    }
    return prefs;
}

private MyPrefs(Context context) {
    this.context = context;
    this.systemPrefs = context.getSharedPreferences(Constants.USER_PREFS, Activity.MODE_PRIVATE);
    this.editor = systemPrefs.edit();
}

public String getDefaultLocation(){
    return systemPrefs.getString(DEFAULT_LOCATION, SOME_DEFAULT_LOCATION);
}

public void setDefaultLocation(String newLocation){
    editor.putString(DEFAULT_LOCATION, newLocation);
    editor.commit();
}

public void clear() {
    editor.clear();
    editor.commit();
}
}
        MyPrefs myPrefs = MyPrefs.getInstance(this);
    //To get the location stored in prefs
    String locationFromPrefs = myPrefs.getDefaultLocation();

    //To store the location in prefs
    myPrefs.setDefaultLocation("New York");