当我打开应用程序或进行其他活动时,我正在尝试保存复选框的选择,这是一个简单的程序,显示可以设置为收藏夹的国家和标志的列表,我只是看不到哪里我在这里错了:
public class MyAdapter extends ArrayAdapter<String> {
String[] names;
int[] flags;
Context mContext;
public MyAdapter(Context context, String[] countryNames, int[] countryFlags, String[] countryDetails) {
super(context, R.layout.listview_item);
this.names = countryNames;
this.flags = countryFlags;
this.mContext = context;
}
@Override
public int getCount() {
return names.length;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
boolean shouldBeChecked = sp.getBoolean(names[position], false);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
mViewHolder.mCheckBox = convertView.findViewById(R.id.check_Box);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mContext.getSharedPreferences("FreshStart",0);
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);
mViewHolder.mCheckBox.setTag(names[position]);
convertView.setTag(mViewHolder);
mViewHolder.mCheckBox.setChecked(shouldBeChecked);
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
buttonView.setSelected(true);
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
}
else{
buttonView.setSelected(false);
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mName;
CheckBox mCheckBox;
}
}
它似乎是我目前最苦恼的if(isChecked)
部分,我知道我需要保存实例状态,并且尝试使用SharedPreferences
保存但不是完全保存确定如何使用它,并希望在此方面有所帮助,以帮助更多地了解它。
答案 0 :(得分:1)
public class MyAdapter extends ArrayAdapter<String> {
String[] names;
int[] flags;
Context mContext;
public MyAdapter(Context context, String[] countryNames, int[] countryFlags, String[] countryDetails) {
super(context, R.layout.listview_item);
this.names = countryNames;
this.flags = countryFlags;
this.mContext = context;
}
@Override
public int getCount() {
return names.length;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder mViewHolder = new ViewHolder();
final SharedPreferences sp = mContext.getSharedPreferences("FreshStart", 0);
boolean shouldBeChecked = sp.getBoolean(names[position], false);
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.listview_item, parent, false);
mViewHolder.mFlag = (ImageView) convertView.findViewById(R.id.imageView);
mViewHolder.mName = (TextView) convertView.findViewById(R.id.textView);
mViewHolder.mCheckBox = convertView.findViewById(R.id.check_Box);
} else {
mViewHolder = (ViewHolder) convertView.getTag();
}
mContext.getSharedPreferences("FreshStart",0);
mViewHolder.mFlag.setImageResource(flags[position]);
mViewHolder.mName.setText(names[position]);
mViewHolder.mCheckBox.setTag(names[position]);
convertView.setTag(mViewHolder);
mViewHolder.mCheckBox.setChecked(shouldBeChecked);
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
SharedPreferences.Editor preferencesEditor = sp.edit();
preferencesEditor.putBoolean("FreshStart", isChecked);
preferencesEditor.apply();
buttonView.setSelected(isChecked);
if (isChecked)
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
else
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
static class ViewHolder {
ImageView mFlag;
TextView mName;
CheckBox mCheckBox;
}
}
答案 1 :(得分:1)
这样做:
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); //mContext is your context
final SharedPreferences.Editor editor = prefs.edit();
buttonView.setSelected(true);
if (isChecked){
Toast.makeText(mContext, "Saved as Favorite", Toast.LENGTH_SHORT).show();
editor.putBoolean("some_name",true); //
editor.apply();
}
else{
Toast.makeText(mContext, "No Longer set as Favorite", Toast.LENGTH_SHORT).show();
editor. editor.putBoolean("some_name",false);
editor.apply();
}
}
希望有帮助。
答案 2 :(得分:0)
mViewHolder.mCheckBox.setChecked(getSharedBoolean(context,"my_switch_key",false));
mViewHolder.mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// buttonView.setSelected(true); // No need to setSelected as callback fired after button check status already change.
storeSharedItem(context,"storeSharedItem",isChecked);
}
}
});
/*
* RCHIVED_SHARED_PREF "String" the Desired preferences file. If a preferences file by this name
* does not exist, it will be created when you retrieve an editor
*/
private final static ARCHIVED_SHARED_PREF = "shared_pref"; // use any String to define SharedPreference storage.
public static boolean getSharedBoolean(Context ctx, String key,boolean def) {
return ctx.getSharedPreferences(ARCHIVED_SHARED_PREF, 0).getBoolean(key, def);
}
public static void storeSharedItem(Context ctx, String key, boolean value) {
SharedPreferences sharedPref = ctx.getSharedPreferences(ARCHIVED_SHARED_PREF, 0);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(key, value);
editor.apply();
}