title: Show data from database by using ListView on Android
body: <p>I am a beginner and I am studying the android at University.</p>
  
  <p>Currently, I am using DBAdapter at the moment, and I am trying to show the data on layout from database by using ListView(if there are better way instead, advice me).</p>
  
  <p>When I run application at the moment, makeText function will be called, and it will be shown data information.</p>
  
  <p>Instead of showing the notification, I want to show some of values on ListView such as name, and quantity.</p>
  
  <p>How can I get it? which ListView do I have to use it with database?
  I want to know the handling database with ListView.
  Please advice.</p>
  
  <p>Code attached below:</p>
  
  <p><strong>FridgeDbAdapter.java</strong></p>
  
  <pre><code>public class FridgeDbAdapter
  {
  public static final String KEY_ROWID = "_id";
  public static final String KEY_CATEGORY = "category";
  public static final String KEY_NAME = "name";
  public static final String KEY_EXPIRED_DATE = "expired_date";
  private static final String DATABASE_TABLE = "fridge_table";
  private Context ctxt;
  private SQLiteDatabase db;
  private FridgeDatabaseHelper dbhelper;
  
  
  public FridgeDbAdapter(Context ctxt)
  {
      this.ctxt = ctxt;
  }
  
  //DB databaseHelper
  public class FridgeDatabaseHelper extends SQLiteOpenHelper
  {
      private static final String DATABASE_NAME = "fridge_db";
      private static final int DATABASE_VERSION = 1;
      private static final String DATBASE_CREATE = 
              "create table fridge_table (_id integer primary key autoincrement, "
   + "category text not null, name text not null, expired_date text not null);";
  
      public FridgeDatabaseHelper(Context ctxt)
      {
          super(ctxt, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db)
      {
          db.execSQL(DATBASE_CREATE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer)
      {
          Log.w(FridgeDatabaseHelper.class.getName(), 
                      "Upgrading database from version " + oldVer + " to "
          + newVer + ", which will delete the old data.");
          db.execSQL("DROP TABLE IF EXISTS fridge_table");
          //Method is called during creation of new database
          onCreate(db);
      }
  }
  
  //Open database
  public FridgeDbAdapter open() throws SQLException
  {
      dbhelper = new FridgeDatabaseHelper(ctxt);
      db = dbhelper.getWritableDatabase();
      return this;
  }
  
  //Close database
  public void close(){
      dbhelper.close();
  }
  
  //Create a new item
  public long insertItem(String category, String name, String expired_date)
  {
      ContentValues initialValues = new ContentValues();
      initialValues.put(KEY_CATEGORY, category);
      initialValues.put(KEY_NAME, name);
      initialValues.put(KEY_EXPIRED_DATE, expired_date);
      return db.insert(DATABASE_TABLE, null, initialValues);
  }
  
  //update a item
  public boolean updateItem(long rowId, String category, 
      String name, String expired_date)
  {
      ContentValues updateValues = new ContentValues();
      updateValues.put(KEY_CATEGORY, category);
      updateValues.put(KEY_NAME, name);
      updateValues.put(KEY_EXPIRED_DATE, expired_date);
      return db.update(DATABASE_TABLE, updateValues, KEY_ROWID + "=" + rowId,
              null) &gt; 0;      
  }
  
  //delete a item
  public boolean deleteItem(long rowId){
      return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) &gt; 0;
  }
  
  //return cursor over the list of all items in the database
  public Cursor fetchAllItems(){
      return db.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_CATEGORY, 
                              KEY_NAME, KEY_EXPIRED_DATE}, 
                     null, null, null, null, null);
  }
  
  //return a cursor positioned at the defined item
  public Cursor fetchItem(long rowId) throws SQLException{
      Cursor mCursor = db.query(true, DATABASE_TABLE, 
              new String[]{KEY_ROWID, KEY_CATEGORY, KEY_NAME,
      KEY_EXPIRED_DATE}, KEY_ROWID + "=" + rowId, null, null, null, null, null);
      if(mCursor != null){
          mCursor.moveToFirst();
      }
      return mCursor;
  }
  }
  </code></pre>
  
  <p><strong>Fridge.java</strong></p>
  
  <pre><code>public class Fridge extends Activity{
  //Button goBack;
  Button button1;
  TextView text;
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.fridge);
  
      FridgeDbAdapter db = new FridgeDbAdapter(this);
  
    //Open database
      db.open();
     //Get all items
     Cursor c = db.fetchAllItems();
      if(c.moveToFirst())
      {
          do
          {
              //Call displayItem method in below
              DisplayItem(c);
          } while (c.moveToNext());
      }
      else
      {
          Toast.makeText(this, "No item found", Toast.LENGTH_SHORT).show();
      }
      db.close();
   }  
  
  public void DisplayItem(Cursor c){
      Toast.makeText(this, "id: " + c.getString(0) + "\n" + 
                      "category: " + c.getString(1) + "\n" +
                      "name: " + c.getString(2) + "\n" + 
                      "expired date: " + c.getString(3), 
                                              Toast.LENGTH_SHORT).show();
  }
  
  
  }
  </code></pre>
  
  <p><strong>fridge.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Fridger page" /&gt;
  
   &lt;LinearLayout
      android:id="@+id/fridge_List_View"
      android:layout_width="match_parent"
      android:layout_height="wrap_content" &gt;
   &lt;/LinearLayout&gt;
  
   &lt;Button
      android:id="@+id/add_Btn"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Add item" /&gt;
  
  
   &lt;/LinearLayout&gt;
  </code></pre>