我一直在尝试以下方法让textview更新对SharedPreference所做的任何更改,但是我很难搞清楚我的下一步行动。任何人? :)
public class CallForwardActivity extends Activity implements
OnSharedPreferenceChangeListener {
SharedPreferences userInfo;
Context context;
UserController userControl;
private static final String USERINFO_FILE = "StoredUserInfo";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.callforward);
context = this;
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userControl = new UserController(context);
final TextView setActiveNumberTV = (TextView)
findViewById(R.id.activeNumberTextView);
setActiveNumberTV.setText(userControl.GetActiveNumber());
final RelativeLayout showAvailablenumbers = (RelativeLayout)
findViewById(R.id.showAvailableNumbers);
showAvailablenumbers.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(getParent(),
AvailableNumbersActivity.class);
SettingsActivityGroup parentActivity =
(SettingsActivityGroup) getParent();
parentActivity.ReplaceView(
"Show Available Numbers Activity", i);
}
});
}
public void onSharedPreferenceChanged(SharedPreferences arg0,
String arg1) {
final TextView setActiveNumberTV = (TextView)
findViewById(R.id.activeNumberTextView);
setActiveNumberTV.setText("...");
}
}
答案 0 :(得分:4)
您没有注册OnPreferenceChangedListener。在userInfo.registerOnSharedPreferenceChangeListener(this)
中致电onCreate
,它应该有效。
答案 1 :(得分:2)
您不会在任何时候将sharedPreferenceChangedListener添加到您的sharedPreferences。
试试这个:
userInfo.registerOnSharedPreferenceChangeListener(this);
在您实例化userInfo对象之后。
答案 2 :(得分:2)
只是为了尝试...
public class CallForwardActivity extends Activity implements OnSharedPreferenceChangeListener
{
SharedPreferences userInfo;
Context context;
UserController userControl;
private static final String USERINFO_FILE = "StoredUserInfo";
private TextView setActiveNumberTV;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.callforward);
context = this;
userInfo = this.getSharedPreferences(USERINFO_FILE, 0);
userInfo.registerOnSharedPreferenceChangeListener(this);
userControl = new UserController(context);
setActiveNumberTV = (TextView) findViewById(R.id.activeNumberTextView);
setActiveNumberTV.setText(userControl.GetActiveNumber());
final RelativeLayout showAvailablenumbers = (RelativeLayout) findViewById(R.id.showAvailableNumbers);
showAvailablenumbers.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
Intent i = new Intent(getParent(), AvailableNumbersActivity.class);
SettingsActivityGroup parentActivity = (SettingsActivityGroup)getParent();
parentActivity.ReplaceView("Show Available Numbers Activity", i);
}
});
}
public void onSharedPreferenceChanged(SharedPreferences arg0, String arg1)
{
setActiveNumberTV.setText(arg0.getString(arg1, "default"));
}
}