Skip to content Skip to sidebar Skip to footer

How To Save Boolean States From An Array Of Checkboxes And Load Their States When The Adapter Is Loaded Using Sharedpreferences

I have a CustomAdapter for a listview and I need to save all checkbox states from an array of boolean using SharedPreferences, I would like to save the name of the trick (an Array

Solution 1:

Try this adapter code:

publicclassCustomAdapter0extendsBaseAdapter {
publicCustomAdapter0(String listname, String[] tricks, Context context) {
    this.tricks = tricks;
    this.context = context;
    isClicked = newboolean[tricks.length];
    for(inti=0; i < isClicked.length; i++) isClicked[i] = false;
    this.listname = listname;
}

private String[] tricks;
private Context context;
privateboolean[] isClicked;
private LayoutInflater layoutInflater;
private String listname;

@OverridepublicintgetCount() {
    return tricks.length;
}

@Overridepublic Object getItem(int i) {
    return tricks[i];
}

@OverridepubliclonggetItemId(int i) {
    return i;
}

@Overridepublic View getView(finalint i, View convertView, ViewGroup viewGroup) {

    Viewrow= convertView;
    if(convertView == null){
        layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = layoutInflater.inflate(R.layout.custom_listview_tricks, null);
    }
    TextViewtextView= row.findViewById(R.id.name_xml);
    ImageButtonimageButton= row.findViewById(R.id.unmastered_xml);

    textView.setText(tricks[i]);
    if (isClicked[i]) imageButton.setBackgroundResource(R.drawable.mastered);
    else imageButton.setBackgroundResource(R.drawable.unmastered);

    imageButton.setOnClickListener(newView.OnClickListener() {
        @OverridepublicvoidonClick(View view) {
            ImageButtonclickedView= (ImageButton) view;
            intclickedPosition= (int)clickedView.getTag();
            isClicked[clickedPosition] = !isClicked[clickedPosition];
            notifyDataSetChanged();

            storeArray();
        }
    });
    imageButton.setTag(i);
    return row;
}

publicbooleanstoreArray() {
    SharedPreferencesprefs= context.getSharedPreferences(listname, Context.MODE_PRIVATE);
    SharedPreferences.Editoreditor= prefs.edit();
    for(inti=0; i < tricks.length; i++)
        editor.putBoolean(tricks[i], isClicked[i]);
    return editor.commit();
}

publicvoidloadArray() {
    SharedPreferencesprefs= context.getSharedPreferences(listname, Context.MODE_PRIVATE);
    for(inti=0; i < tricks.length; i++)
        isClicked[i] = prefs.getBoolean(tricks[i], false);
}
}

In Activity, changes those if-statements for initialize the adapter like:

if (codigo == 0){
        CustomAdapter0customAdapter0=newCustomAdapter0("lower", lower, TricksActivity.this);
        listView.setAdapter(customAdapter0);
        customAdapter0.loadArray();

    }if (codigo == 1){

        CustomAdapter0customAdapter0=newCustomAdapter0("upper", upper, TricksActivity.this);
        listView.setAdapter(customAdapter0);
        customAdapter0.loadArray();

    }if (codigo == 2){

        CustomAdapter0customAdapter0=newCustomAdapter0("sitDown", sitDown, TricksActivity.this);
        listView.setAdapter(customAdapter0);
        customAdapter0.loadArray();

    }if (codigo == 3){

        CustomAdapter0customAdapter0=newCustomAdapter0("combosFamosos", combosFamosos, TricksActivity.this);
        listView.setAdapter(customAdapter0);
        customAdapter0.loadArray();
    }

Hope that helps!

Post a Comment for "How To Save Boolean States From An Array Of Checkboxes And Load Their States When The Adapter Is Loaded Using Sharedpreferences"