user3343264
user3343264

Reputation: 71

saving checkbox states from list with shared preferences

I asked a question on how to save checkbox state using a textfile but was recommended to use shared preferences. I had tried this method before using textfiles but had the same issue - checkboxes in the list check and uncheck seemingly randomly.

My app displays a list of currently installed apps on the device, it displays the title of the app, the icon and a checkbox. If I check one of the items and scroll down and back up again, it will often uncheck and other items are checked randomly. I'm trying to get it so the checkbox states are loaded from shared preferences, and saved to when the user manually checks the box.

Here is the code for the listview custom adapter:

    public class AppDetailsAdapter extends BaseAdapter {

private List<AppDetails> data;
private Context context;


public AppDetailsAdapter(Context context, List<AppDetails> data) {
    this.context = context;
    this.data = data;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;

    if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }

    ImageView icon = (ImageView) view.findViewById(R.id.icon);
    TextView text = (TextView) view.findViewById(R.id.text);
    final CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox);
    final AppDetails item = data.get(position);

    text.setText(item.name);
    icon.setImageDrawable(item.icon);

    SharedPreferences settings = context.getSharedPreferences("data",Context.MODE_PRIVATE);
    boolean Checked = settings.getBoolean(item.name, false);
    checkBox.setChecked(Checked);

    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // Handle your conditions here
        if(checkBox.isChecked()==true){

            SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
            settings.edit().putBoolean(item.name,true).commit();
            Toast.makeText(context,"You selected "+item.name, Toast.LENGTH_SHORT).show();
        }
        else {
            SharedPreferences settings = context.getSharedPreferences("data", Context.MODE_PRIVATE);
            settings.edit().putBoolean(item.name,false).commit();
            Toast.makeText(context,"You deselected "+item.name, Toast.LENGTH_SHORT).show();
        }




    }
    });


    return view;
}

}

Upvotes: 2

Views: 2253

Answers (1)

Setu Kumar Basak
Setu Kumar Basak

Reputation: 12022

This is happening because of view recycling.

Can u try one thing:
Instead of this

   if (view == null) {
        LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = vi.inflate(R.layout.custom_row_layout, null);
    }

just write

  LayoutInflater vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = vi.inflate(R.layout.custom_row_layout, null);

run again.

Upvotes: 2

Related Questions