title: How to effectively store checkbox values in Custom BaseAdapter
body: <p>For the past two weeks, I have been struggling to produce a solution to store my checkbox values next to each of my listview items when I exit and open the app. Essentially, I have a custom BaseAdapter class which populates the listview with installed applications on the users phone and a checkbox next to each installed app. When I scroll down the checkbox states stay;however, when I click back or exit and come back it doesn't stay. </p>
  
  <p><img src="https://i.stack.imgur.com/MYe84.png" alt="enter image description here"></p>
  
  <p>I used SharedPreferences in almost every way possible. My last way was using a for loop to create a SharedPreference for each one and restore the boolean in the for loop as well and to store a boolean true and false (for checked and unchecked respectively) in if and else statements. </p>
  
  <p>Here is my code for my BaseAdapter class using SharedPreferences:</p>
  
  <pre><code>package com.ibc.android.demo.appslist.app;
  
  import android.app.Activity;
  import android.content.Context;
  import android.content.SharedPreferences;
  import android.content.pm.PackageInfo;
  import android.content.pm.PackageManager;
  import android.graphics.drawable.Drawable;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.CheckBox;
  import android.widget.TextView;
  
  import com.spicycurryman.getdisciplined10.app.R;
  
  import java.util.List;
  
  //
  
  public class ApkAdapter extends BaseAdapter {
  
      //Pastebin link:  http://pastebin.com/LGRicg4U , http://pastebin.com/c4WfmhMK , http://pastebin.com/gFuuM4dY, http://pastebin.com/4Q7EP9G4
      // http://pastebin.com/Te2g072w,  http://pastebin.com/NLT5iUiA ,
  
      SharedPreferences sharedPrefs;
      List&lt;PackageInfo&gt; packageList;
      Activity context;
      PackageManager packageManager;
      boolean[] itemChecked;
  
      String PACKAGE_NAME;
  
      public ApkAdapter(Activity context, List&lt;PackageInfo&gt; packageList,
                        PackageManager packageManager) {
          super();
          this.context = context;
          this.packageList = packageList;
          this.packageManager = packageManager;
          itemChecked = new boolean[packageList.size()];
  
      }
  
  
      private class ViewHolder {
          TextView apkName;
          CheckBox ck1;
          TextView packageName;
      }
  
      public int getCount() {
          return packageList.size();
      }
  
      public Object getItem(int position) {
          return packageList.get(position);
      }
  
      public long getItemId(int position) {
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          final ViewHolder holder;
  
          LayoutInflater inflater = context.getLayoutInflater();
  
          if (convertView == null) {
              convertView = inflater.inflate(R.layout.installed_apps, null);
              holder = new ViewHolder();
  
              holder.apkName = (TextView) convertView
                      .findViewById(R.id.appname);
              holder.ck1= (CheckBox)convertView
                      .findViewById(R.id.checkBox1);
  
  
  
              convertView.setTag(holder);
              //holder.ck1.setTag(packageList.get(position));
  
          } else {
  
              holder = (ViewHolder) convertView.getTag();
          }
  
  
  
          // ViewHolder holder = (ViewHolder) convertView.getTag();
          PackageInfo packageInfo = (PackageInfo) getItem(position);
  
  
  
          Drawable appIcon = packageManager
                  .getApplicationIcon(packageInfo.applicationInfo);
  
  
  
          // Make sure to define it again!
          PACKAGE_NAME = packageInfo.packageName;
  
  
          final String appName = packageManager.getApplicationLabel(
                  packageInfo.applicationInfo).toString();
          appIcon.setBounds(0, 0, 80, 80);
          holder.apkName.setCompoundDrawables(appIcon, null, null, null);
          holder.apkName.setCompoundDrawablePadding(15);
          holder.apkName.setText(appName);
  
  
          holder.ck1.setChecked(false);
  
  
          if (itemChecked[position])
              holder.ck1.setChecked(true);
          else
              holder.ck1.setChecked(false);
  
          Log.d("just loaded??", PACKAGE_NAME);
  
  
          Log.d("just loaded 2?", appName+position);
  
  
  
          // CHANGE UP EVERYTHING! MAKE THIS SHIT WORK, TIGGA!
  
  
          for(int i= 0; i&lt;packageList.size(); i++){
              sharedPrefs = context.getSharedPreferences(String.valueOf(i), Context.MODE_PRIVATE);
              holder.ck1.setChecked(sharedPrefs.getBoolean(String.valueOf(i),false));
  
  
          }
  
          holder.ck1.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
  
  
                  SharedPreferences.Editor editor = context.getSharedPreferences(String.valueOf(position), Context.MODE_PRIVATE).edit();
  
                  if (holder.ck1.isChecked()) {
                      itemChecked[position] = true;
                      holder.ck1.setChecked(true);
                      Log.i("This is", " checked: " + position);
                      editor.putBoolean(String.valueOf(position), true);
                      Log.d("put true", appName+position);
  
                      editor.apply();
  
                  } else {
                      itemChecked[position] = false;
                      holder.ck1.setChecked(false);
                      Log.i("This is", " not checked: " + position);
                      editor.putBoolean(String.valueOf(position), false);
                      Log.d("put false", appName+position);
  
                      editor.apply();
  
                  }
  
              }
  
  
          });
  
          return convertView;
  
      }
  
  
  
  
  }
  </code></pre>
  
  <p>I have also created a DatabaseHandler class and Object class for using SQLite database to store my checkbox values instead after unsuccessfully storing it with SharedPreferences.</p>
  
  <p>Here are the pastebin links so I don't clog the post:</p>
  
  <p>DatabaseHandler class: <a href="http://pastebin.com/NzKhBiZ3" rel="nofollow noreferrer">http://pastebin.com/NzKhBiZ3</a></p>
  
  <p>Object class:  <a href="http://pastebin.com/Jp3BLXba" rel="nofollow noreferrer">http://pastebin.com/Jp3BLXba</a></p>
  
  <p>I know that there are many posts, blogs, and links on using custom adapters/listviews and saving checkboxes and button states etc using SQLite and SharedPreferences, but I assure you I  have done my fair share of research for weeks, but I haven't been able to find something that works for my specific case.  </p>
  
  <p>Some examples:</p>
  
  <p><a href="https://stackoverflow.com/questions/12621804/saving-state-of-the-checkbox-in-a-custom-list-view-with-checkboxes?rq=1">Saving State of the checkbox in a custom list view with Checkboxes</a></p>
  
  <p><a href="http://blog.csdn.net/qu213/article/details/9289349" rel="nofollow noreferrer">http://blog.csdn.net/qu213/article/details/9289349</a></p>
  
  <p><em>What would be the most optimal option in my particular case and How would I produce a solution in my situation to successfully save the checkbox states next to each installed app listview item that would be saved after I exit the application and re-enter? Some code would really be of help.</em></p>
  
  <p>Thank you for your time. </p>
  
  <p>Let me know if you need me to include any xml files.</p>