2
votes

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;


    }


}


4
you mean you want ot do it for all the items in the list?Chol
bro please check my answersaeed
You need to store each value in the database, you cannot use preference as you will save only one value and not all the value for each itemChol

4 Answers

2
votes
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.
1
votes

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);
0
votes

If i got it correct Store the checked item in database of some where with the unique key(like;- name if it is unique).. get the list of checked though constructor and check in getview() if exists than make it checked otherwise make it unchecked.. You can use shared preferences too.. the both techniques need a unique value.

0
votes

Saving a particular state is very easy with Booleans and a switch statement inside an onClick.

private Boolean toggleBtn1;
private Boolean toggleBtn2;
private Boolean isChecked;

In your 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;

                        }

                }

            }
        });

Save the boolean outside the getView.