我想在我正在开发的viewpager中存储按钮收藏状态,以便用户可以随时回顾他们已经预订为收藏夹的图像。存储按钮状态,但是,一旦我重新打开应用程序,按钮状态就没有改变。是因为活动被破坏了吗?如何在viewpager中存储按钮的状态?
@Override
public Object instantiateItem(final ViewGroup container, final int position) {
showProgress();
imageView = (ImageView) findViewById(R.id.btn_favourite);
imageView.setColorFilter(Color.argb(255, 192, 192, 192));
imageView.setOnClickListener(new View.OnClickListener() {
Boolean stateBtn= sharedPreference.getBtnState(context);
@Override
public void onClick(View v) {
// Boolean stateBtn= sharedPreference.getBtnState(context);
if(!stateBtn) {
sharedPreference.save(context, mUrl);
sharedPreference.saveBtnState(context, stateBtn);
Toast.makeText(context,
"Added to Favourite!",
Toast.LENGTH_SHORT).show();
imageView.setColorFilter(Color.argb(255, 249, 0, 0));
}
else
{
sharedPreference.saveBtnState(context, stateBtn);
imageView.setColorFilter(Color.argb(255, 192, 192, 192));
}
}
});
View photoRow = inflater.inflate(R.layout.item_image, container,
false);
ImageView image = (ImageView) photoRow.findViewById(R.id.img_flickr);
// added imageloader for better performance
StaggeredDemoApplication.getImageLoader().get(imageArrayList[position],
ImageLoader.getImageListener(image, R.drawable.bg_no_image, android.R.drawable.ic_dialog_alert), container.getWidth(), 0);
((ViewPager) container).addView(photoRow);
stopProgress();
return photoRow;
}
以下是共享偏好的代码
public class SharedPreference {
public static final String PREFS_NAME = "AOP_PREFS";
public static final String PREFS_STATE="AOP_BTN";
public static final String PREFS_KEY = "AOP_PREFS_String";
public static final String PREF_BTN_KEY = "AOP_PREF_BTN";
public SharedPreference() {
super();
}
public void save(Context context, String text) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
editor = settings.edit(); //2
editor.putString(PREFS_KEY, text); //3
editor.commit(); //4
}
public String getValue(Context context) {
SharedPreferences settings;
String text;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
text = settings.getString(PREFS_KEY, null);
return text;
}
public void clearSharedPreference(Context context) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.clear();
editor.commit();
}
public void removeValue(Context context) {
SharedPreferences settings;
Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.remove(PREFS_KEY);
editor.commit();
}
public void saveBtnState(Context context, Boolean stateBtn) {
SharedPreferences settings;
Editor editor;
//settings = PreferenceManager.getDefaultSharedPreferences(context);
settings = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE); //1
editor = settings.edit(); //2
editor.putBoolean(PREF_BTN_KEY, stateBtn);//added state for button
editor.commit(); //4
}
public boolean getBtnState(Context context)
{
SharedPreferences prefs = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE);
boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false);
return switchState;
}
}
答案 0 :(得分:3)
PREFS_STATE
代替PREFS_NAME
,并使用PREF_BTN_KEY
代替PREFS_NAME
。这是因为在保存按钮状态时,您使用了键PREFS_STATE
的首选项,并将布尔值设置为PREF_BTN_KEY
。
public boolean getBtnState(Context context)
{
SharedPreferences prefs = context.getSharedPreferences(PREFS_STATE, Context.MODE_PRIVATE);
boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false);
return switchState;
}
答案 1 :(得分:1)
更改 getBtnState 方法
public boolean getBtnState(Context context)
{
SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
boolean switchState = prefs.getBoolean(PREF_BTN_KEY, false);
return switchState;
}