Saving ToggleButton state in ListView by using SharedPreferences
I want to save my ToggleButton state by SharedPreferences at this below codes. Please guide me. Thanks so much.
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; } }
如果我得到它正确的存储在某些地方与唯一键的数据库中的检查项目(如; - 名称,如果它是唯一的)..获取列表检查通过构造函数和检查getview()
如果存在比让它检查,否则使其不受检查..你也可以使用共享偏好..这两种技术都需要一个独特的价值。
you can save toggle button state using below code
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();
}
});
to get them back use
Boolean value = prefs.getBoolean("value", false);
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.
链接地址: http://www.djcxy.com/p/50608.html
上一篇: 多个可配置的小部件