gpt4 book ai didi

Android 按钮状态未存储在 viewpager 中

转载 作者:行者123 更新时间:2023-11-29 15:44:27 25 4
gpt4 key购买 nike

我想在我正在开发的 viewpager 中存储按钮收藏状态,以便用户始终可以回顾他们已标记为收藏的图像。按钮状态已存储,但是,一旦我重新打开应用程序,按钮状态就没有改变。是不是因为activity被破坏了?如何在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;
}
}

最佳答案

否,因为您调用的偏好设置错误。您必须使用 PREFS_STATE 而不是 PREFS_NAME 并且还必须使用 PREF_BTN_KEY 而不是 PREFS_NAME 。这是因为在保存按钮状态时,您正在使用键 PREFS_STATE 的首选项并将 bool 值与 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;
}

关于Android 按钮状态未存储在 viewpager 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35054147/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com