gpt4 book ai didi

java - 保存复选框的选择

转载 作者:行者123 更新时间:2023-12-02 02:00:26 24 4
gpt4 key购买 nike

当我打开应用程序或转到其他 Activity 时,我试图保存复选框的选择,这是一个简单的程序,显示您可以设置为 Collection 夹的国家和国旗列表,我只是看不到在哪里我这里出错了:

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保存但不完全确定如何使用它,并希望获得一些帮助以帮助更多地理解它

最佳答案

这样做:

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();
}

}

希望有帮助。

关于java - 保存复选框的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656971/

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