title: Using a SQlite database to store checkbox states in custom baseadapter listview
body: <p>I am using a custom BaseAdapter listview to display a list of installed apps with a checkbox next to it. I am trying to save the each listview item and checkbox state in an SQLite database after attempting to do so with SharedPreferences. </p>
  
  <p>I would like it to store the state of the checkbox once the listview has been closed and once the application has been closed aswell. </p>
  
  <p><img src="https://i.stack.imgur.com/F3JyT.png" alt="enter image description here"></p>
  
  <p>I have done much research and have been at this for well over a week, but haven't been able to produce or find a solution that works for me. </p>
  
  <p><a href="https://stackoverflow.com/questions/23144447/how-to-save-state-of-checkboxes-in-database-both-true-and-false-states">How to save state of checkboxes in database (both true and false states)</a></p>
  
  <p><a href="https://stackoverflow.com/questions/7610267/storing-the-state-of-checkbox-in-listview-while-using-custom-baseadapter-in-andr">Storing the state of checkbox in listview while using custom BaseAdapter in android?</a></p>
  
  <p>Here is my BaseAdapter class:</p>
  
  <pre><code>package com.ibc.android.demo.appslist.app;
  
  import android.app.Activity;
  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
  
      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;
      }
  
      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);
  
          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??", appName);
  
  
          Log.d("just loaded 2?", appName);
  
  
          holder.ck1.setOnClickListener(new OnClickListener() {
              @Override
              public void onClick(View v) {
  
                 /* sharedPrefs = context.getSharedPreferences("apps", Context.MODE_PRIVATE);
                  holder.ck1.setChecked(sharedPrefs.getBoolean(appName,false));
                  SharedPreferences.Editor editor = context.getSharedPreferences(appName, Context.MODE_PRIVATE).edit();*/
  
                  if (holder.ck1.isChecked()) {
                      itemChecked[position] = true;
                      holder.ck1.setChecked(true);
                      Log.i("This is", " checked: " + position);
                      //editor.putBoolean(appName, true);
                      Log.d("put true", appName);
  
                      //editor.apply();
  
                  } else {
                      itemChecked[position] = false;
                      holder.ck1.setChecked(false);
                      Log.i("This is", " not checked: " + position);
                      //editor.putBoolean(appName, false);
                      Log.d("put false", appName);
  
                      //editor.apply();
  
                  }
  
              }
  
          });
  
  
          return convertView;
  
      }
  
  }
  </code></pre>
  
  <p>Here is my DataBaseHandler class:</p>
  
  <pre><code>package com.ibc.android.demo.appslist.app;
  
  /**
   * Created by Spicycurryman on 8/5/14.
   */
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class DatabaseHandler extends SQLiteOpenHelper {
  
      // All Static variables
      // Database Version
      private static final int DATABASE_VERSION = 1;
  
      // Database Name
      private static final String DATABASE_NAME = "packagenameManager";
  
      // Packagename table name
      private static final String TABLE_PACKAGE_NAME = "packagenames";
  
      // Contacts Table Columns names
      private static final String KEY_PACKAGE_NAME = "packagenames";
      private static final String KEY_STATE = "state";
  
  
      public DatabaseHandler(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      // Creating Tables
      @Override
      public void onCreate(SQLiteDatabase db) {
          String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_PACKAGE_NAME + "("
                  + KEY_PACKAGE_NAME + " STRING PRIMARY KEY," + KEY_STATE + " INT,"
                   + ")";
          db.execSQL(CREATE_CONTACTS_TABLE);
      }
  
      // Upgrading database
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          // Drop older table if existed
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_PACKAGE_NAME);
  
          // Create tables again
          onCreate(db);
      }
  
      /**
       * All CRUD(Create, Read, Update, Delete) Operations
       */
  
      // Adding new packagename
      void addPackageName(PackageName packageName) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_STATE, packageName.getPACKAGENAME()); // Packagename
  
          // Inserting Row
          db.insert(TABLE_PACKAGE_NAME, null, values);
          db.close(); // Closing database connection
      }
  
      // Getting single package
      PackageName getPackageName(String packagename) {
          SQLiteDatabase db = this.getReadableDatabase();
  
          Cursor cursor = db.query(TABLE_PACKAGE_NAME, new String[] {KEY_PACKAGE_NAME,
                          KEY_STATE }, KEY_PACKAGE_NAME + "=?",
                  new String[] { packagename }, null, null, null, null);
          if (cursor != null)
              cursor.moveToFirst();
  
          PackageName packageName = new PackageName((cursor.getString(0)),
                  cursor.getInt(1));
          // return packagename
          return packageName;
      }
  
      // Getting All packagenames
      public List&lt;PackageName&gt; getAllPackageNames() {
          List&lt;PackageName&gt; packageNameList = new ArrayList&lt;PackageName&gt;();
          // Select All Query
          String selectQuery = "SELECT  * FROM " + TABLE_PACKAGE_NAME;
  
          SQLiteDatabase db = this.getWritableDatabase();
          Cursor cursor = db.rawQuery(selectQuery, null);
  
          // looping through all rows and adding to list
          if (cursor.moveToFirst()) {
              do {
                  PackageName packageName = new PackageName();
                  packageName.setPACKAGENAME(cursor.getString(0));
                  packageName.setState(cursor.getInt(1));
                  // Adding contact to list
                  packageNameList.add(packageName);
              } while (cursor.moveToNext());
          }
  
          // return packagename list
          return packageNameList;
      }
  
      // Updating single packagename
      public int updatePackageName(PackageName packageName) {
          SQLiteDatabase db = this.getWritableDatabase();
  
          ContentValues values = new ContentValues();
          values.put(KEY_STATE, packageName.getState());
  
          // updating row
          return db.update(TABLE_PACKAGE_NAME, values, KEY_PACKAGE_NAME + " = ?",
                  new String[] { String.valueOf(packageName.getPACKAGENAME()) });
      }
  
      // Deleting single packagename
      public void deletePackageName(PackageName packageName) {
          SQLiteDatabase db = this.getWritableDatabase();
          db.delete(TABLE_PACKAGE_NAME, KEY_PACKAGE_NAME + " = ?",
                  new String[] { String.valueOf(packageName.getPACKAGENAME()) });
          db.close();
      }
  
  
      // Getting packagename Count
      public int getPackageNameCount() {
          String countQuery = "SELECT  * FROM " + TABLE_PACKAGE_NAME;
          SQLiteDatabase db = this.getReadableDatabase();
          Cursor cursor = db.rawQuery(countQuery, null);
          cursor.close();
  
          // return count
          return cursor.getCount();
      }
  
  }
  </code></pre>
  
  <p>And lastly here is my Object class:</p>
  
  <pre><code>package com.ibc.android.demo.appslist.app;
  
  /**
   * Created by Spicycurryman on 8/5/14.
   */
  public class PackageName {
  
      //private variables
      String _packagename;
      int _state;
  
      // Empty constructor
      public PackageName(){
  
      }
      // constructor
      public PackageName(String packagename, int state){
          this._packagename = packagename;
          this._state = state;
      }
  
      // constructor
      public PackageName(int state){
          this._state = state;
      }
      // getting PACKAGENAME
      public String getPACKAGENAME(){
          return this._packagename;
      }
  
      // setting packagename
      public void setPACKAGENAME(String packagename){
          this._packagename = packagename;
      }
  
      // getting state
      public int getState(){
          return this._state;
      }
  
      // setting state
      public void setState(int state){
          this._state = state;
      }
  
  
  }
  </code></pre>
  
  <p>How would I go about achieving this?</p>