我从sharedPreference获取了一个Set,我为它添加了一个值,它保存得很好。但是,当我完全关闭应用程序并重新打开...那个价值已经消失了。这个问题不是在我在一个活动中进行时发生的,而是在我的Spinner监听器代码中。
这是我尝试过的:我通过它的构造函数将首选项和编辑器对象从活动传递给了侦听器类。因此,无需初始化任何东西。我仍然得到同样的错误。
另外,我尝试启动Activity的一个实例并保存在那里保存...但是那也没有用。
我无法为您提供任何代码,因为它位于不同的计算机上......但代码不是问题,因为相同的代码在活动中运行良好
更新:已添加代码
public class SpinnerListener1 extends Activity
implements AdapterView.OnItemSelectedListener {
SpinnerAdpter spinnerAdapter1;
Spinner addCtAvail;
String type;
Context c;
SharedPreferences.Editor edit;
SharedPreferences prefs;
// String value;
public SpinnerListener1(SpinnerAdpter vSpinnerAdpter, Spinner vaddCtAvail, String type, Context ctx,SharedPreferences prefs ,SharedPreferences.Editor edit ){
this.spinnerAdapter1 = vSpinnerAdpter;
this.addCtAvail = vaddCtAvail;
this.type = type;
this.c = ctx;
this.prefs = prefs;
this.edit = edit;
}//SpinnerListener1
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selection = ((TextView) view).getText().toString();
if (selection.equals("new")) {
AlertDialog.Builder builder11 = new AlertDialog.Builder(c);
builder11.setTitle("Add a new value to List");
builder11.setMessage("Please Enter a Value");
builder11.setCancelable(true);
builder11.setIcon(R.drawable.ic_launcher);
final EditText input = new EditText(c);
input.setId(0);
builder11.setView(input);
builder11.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Tabs1 tabInstance = new Tabs1();
String newCTvalue = input.getText().toString();
// have the new entry appear in spinner:
spinnerAdapter1.remove("new");
spinnerAdapter1.add(newCTvalue);
spinnerAdapter1.add("new");
addCtAvail.setAdapter(spinnerAdapter1);
addCtAvail.setSelection(spinnerAdapter1.getCount());
saveNewValue(newCTvalue, type);
}
})
.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder11.create().show();
}
}//onItemSelected
public void saveNewValue(String value, String prefname){
Set set = prefs.getStringSet(prefname,null);
set.add(value);
edit.putStringSet(prefname , set);
edit.commit();
}//saveNewValue
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}//SpinnerListener
这是我的TabActivity的代码,它启动了这个列表器:
public void createSpinner(View view, String type, String texttoShow , Spinner addLoadType){
//prefs = PreferenceManager.getDefaultSharedPreferences(Tabs1.this);
prefs = getApplicationContext().getSharedPreferences("SpinnerPrefs", 0);
edit = prefs.edit();
getSpinnerSharedPref(type);
final SpinnerAdpter spinnerAdapterLoadType = new SpinnerAdpter(this);
spinnerAdapterLoadType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SpinnerListener1 spinnerlistenerLoadType = new SpinnerListener1(spinnerAdapterLoadType, addLoadType,type, Tabs1.this, prefs, editor);
addLoadType.setOnItemSelectedListener(spinnerlistenerLoadType);
getSpinnerPrefandPopulate(type,spinnerAdapterLoadType );
spinnerAdapterLoadType.add(texttoShow);
addLoadType.setAdapter(spinnerAdapterLoadType);
addLoadType.setSelection(spinnerAdapterLoadType.getCount()); //display hint