当前位置: 首页 > 知识库问答 >
问题:

使用SharedPreferences保存ListView中的ToggleButton状态

陆宏扬
2023-03-14

我想在下面的代码中通过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;


    }


}


共有2个答案

佟云
2023-03-14

您可以使用下面的代码保存切换按钮状态

   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);
元景天
2023-03-14
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.
 类似资料: