title: Using SQLite cursor to output table contents in listview in a fragment
body: <p>I have <code>MyDBHandler</code> with a <code>getAllDetails</code> method that queries the database using a cursor and returns a list. </p>
  
  <p>What I don't know what to do now is how to output this list in a <code>listview</code> in another fragment. I was told to create two XML layouts and a custom adapter but I don't know how to this exactly!</p>
  
  <p><strong>MyDBHandler class</strong></p>
  
  <pre><code>package com.astuetz.viewpager.extensions.sample;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.database.Cursor;
  import android.content.Context;
  import android.content.ContentValues;
  
  import java.util.ArrayList;
  import java.util.List;
  
  public class MyDBHandler extends SQLiteOpenHelper {
  
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "detailsDB.db";
  public static final String TABLE_DETAILS = "details";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_FIRSTNAME = "firstname";
  public static final String COLUMN_SURNAME = "surname";
  public static final String COLUMN_PHONE = "phone";
  public static final String COLUMN_EMAIL = "email";
  public static final String COLUMN_ADDRESS1 = "address1";
  public static final String COLUMN_ADDRESS2 = "address2";
  
  public static final String TABLE_KIN_DETAILS = "kindetails";
  public static final String COLUMN_KIN_ID = "_id";
  public static final String COLUMN_KIN_YOUREMAIL = "youremailkin";
  public static final String COLUMN_KIN_FIRSTNAME = "firstnamekin";
  public static final String COLUMN_KIN_SURNAME = "surnamekin";
  public static final String COLUMN_KIN_PHONE = "phonekin";
  public static final String COLUMN_KIN_EMAIL = "emailkin";
  public static final String COLUMN_KIN_ADDRESS1 = "address1kin";
  public static final String COLUMN_KIN_ADDRESS2 = "address2kin";
  
  // Pass database information along to superclass
  public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
      super(context, DATABASE_NAME, factory, DATABASE_VERSION);
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      String query = " CREATE TABLE " + TABLE_DETAILS + "("
              + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
              + COLUMN_FIRSTNAME + " TEXT, "
              + COLUMN_SURNAME + " TEXT, "
              + COLUMN_PHONE + " TEXT, "
              + COLUMN_EMAIL + " TEXT, "
              + COLUMN_ADDRESS1 + " TEXT, "
              + COLUMN_ADDRESS2 + " TEXT "
              + ");";
  
      String query2 = " CREATE TABLE " + TABLE_KIN_DETAILS + "("
              + COLUMN_KIN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
              + COLUMN_KIN_YOUREMAIL + " TEXT, "
              + COLUMN_KIN_FIRSTNAME + " TEXT, "
              + COLUMN_KIN_SURNAME + " TEXT, "
              + COLUMN_KIN_PHONE + " TEXT, "
              + COLUMN_KIN_EMAIL + " TEXT, "
              + COLUMN_KIN_ADDRESS1 + " TEXT, "
              + COLUMN_KIN_ADDRESS2 + " TEXT "
              + ");";
      db.execSQL(query);
      db.execSQL(query2);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      db.execSQL(" DROP TABLE IF EXISTS " + TABLE_DETAILS);
      db.execSQL(" DROP TABLE IF EXISTS " + TABLE_KIN_DETAILS);
      onCreate(db);
  }
  
  //Add a new row to the database
  public void addDetails(Details details) {
      ContentValues values = new ContentValues();
      values.put(COLUMN_FIRSTNAME, details.getFirstname());
      values.put(COLUMN_SURNAME, details.getSurname());
      values.put(COLUMN_PHONE, details.getPhone());
      values.put(COLUMN_EMAIL, details.getEmail());
      values.put(COLUMN_ADDRESS1, details.getAddress1());
      values.put(COLUMN_ADDRESS2, details.getAddress2());
      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_DETAILS, null, values);
      db.close();
  }
  
  public void addKinDetails(KinDetails kinDetails){
      ContentValues values = new ContentValues();
      values.put(COLUMN_KIN_YOUREMAIL, kinDetails.getyourEmailkin());
      values.put(COLUMN_KIN_FIRSTNAME, kinDetails.getFirstnamekin());
      values.put(COLUMN_KIN_SURNAME, kinDetails.getSurnamekin());
      values.put(COLUMN_KIN_PHONE, kinDetails.getPhonekin());
      values.put(COLUMN_KIN_EMAIL, kinDetails.getEmailkin());
      values.put(COLUMN_KIN_ADDRESS1, kinDetails.getAddress1kin());
      values.put(COLUMN_KIN_ADDRESS2, kinDetails.getAddress2kin());
      SQLiteDatabase db = getWritableDatabase();
      db.insert(TABLE_KIN_DETAILS, null, values);
      db.close();
  }
  
  
  
  public List&lt;Details&gt; getAllDetails(){
  
      //create a new list in which we put all persons
      List&lt;Details&gt;detailsList = new ArrayList&lt;&gt;();
  
      SQLiteDatabase db = getWritableDatabase();
      String query = "SELECT * FROM " + TABLE_DETAILS;
  
      //Cursor points to a location in your results
      Cursor c = db.rawQuery(query, null);
      //Move to the first row in your results
  
      if (c != null) {
  
          c.moveToFirst();
  
          //Position after the last row means the end of the results
          while (!c.isAfterLast()) {
  
              //create new details object
              Details details = new Details();
  
              //Here use static declared on top of the class..don't use "" for the table column
              details.set_id(c.getColumnIndex(COLUMN_ID));
              details.setFirstname(c.getString(c.getColumnIndex(COLUMN_FIRSTNAME)));
              details.setSurname(c.getString(c.getColumnIndex(COLUMN_SURNAME)));
              details.setPhone(c.getString(c.getColumnIndex(COLUMN_PHONE)));
              details.setEmail(c.getString(c.getColumnIndex(COLUMN_EMAIL)));
              details.setAddress1(c.getString(c.getColumnIndex(COLUMN_ADDRESS1)));
              details.setAddress2(c.getString(c.getColumnIndex(COLUMN_ADDRESS2)));
  
              detailsList.add(details);
  
  
              c.moveToNext();
          }
  
          c.close();
      }
  
      db.close();
  
      //return our list of persons
      return detailsList;
  
  }
  }
  </code></pre>