我想在下面的代码中通过SharedPreferences保存我的ToggleButton状态。 请指导我。 非常感谢。
public class MyAdapterList extends BaseAdapter { private Context context; private List text; public MyAdapterList(Context context, List text) { this.context = context; this.text = text; } @Override public int getCount() { return text.size(); } @Override public Object getItem(int position) { return text.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout.item_view, null, false); TextView tv = (TextView) view.findViewById(R.id.textView); tv.setText(text.get(position)); Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/b_titr.ttf"); tv.setTypeface(tf); tv.setTextColor(Color.parseColor("#003366")); tv.setTypeface(tv.getTypeface(), Typeface.BOLD); final ToggleButton toggleButton = (ToggleButton) view.findViewById(R.id.t_button); final MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(context); toggleButton.setBackgroundResource(R.drawable.favorite_not_selected); toggleButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (toggleButton.isChecked()) { myDatabaseHelper.insert(text.get(position)); toggleButton.setBackgroundResource(R.drawable.favorite_selected); Toast.makeText(context, "Was added to the list of favorites", Toast.LENGTH_SHORT).show(); } else { myDatabaseHelper.deletedData(text.get(position)); Toast.makeText(context, "Removed from favorites", Toast.LENGTH_SHORT).show(); toggleButton.setBackgroundResource(R.drawable.favorite_not_selected); } } }); return view; } }
答案 0 :(得分:1)
您可以使用以下代码保存切换按钮状态
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
prefs.edit().putBoolean("value", isChecked).commit();
}
});
让他们回来使用
Boolean value = prefs.getBoolean("value", false);
答案 1 :(得分:1)
static ArrayList<String> toogleValue=new ArrayList<String>();
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
if(toogleValue.contains("id"))
{
toggle.setChecked(true)
}else
{
toggle.setChecked(false);
}
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
toogleValue.add("id")
}else
{
if(toogleValue.contains(id))
toogleValue.remove("id")
}
}
});
I hope its help you.
答案 2 :(得分:0)
如果我弄错了
将选中的项目存储在具有唯一键的某些位置的数据库中(例如; - 如果它是唯一的名称)..获取已检查的构造函数列表并检查getview()
是否存在而不是检查否则将其取消选中..
您也可以使用共享首选项..这两种技术都需要一个唯一的值。
答案 3 :(得分:0)
通过onClick内的布尔值和switch语句,保存特定状态非常容易。
private Boolean toggleBtn1;
private Boolean toggleBtn2;
private Boolean isChecked;
在您的getView中:
toggleBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isChecked = ((ToggleButton)v).isChecked();
toggleBtn.setTag(position);
String pos = String.valueOf(position);
switch (pos){
case "0" :
if (isChecked){
toggleBtn1 = true;
} else {
toggleBtn1 = false;
}
case "1" :
if (isChecked){
toggleBtn2 = true;
} else {
toggleBtn2 = false;
}
}
}
});
将布尔值保存在getView之外。