title: How to use ListView in order to view SQLite Database in Android Studio?
body: <p>My aim is to get the database values in a ListView. I've never used ListView and don't exactly know how to do it.</p>
  
  <p>So far I have been using an AlertDialog to view the entries of database. But it creates problem as when one entry is added in database and is viewed then it is stored in stack memory and when you add another entry in Database table and then view it, it shows the new entry but after pressing Back button it also shows the previous entry as such.</p>
  
  <p>How can I show entries from database in the ListView ?</p>
  
  <p><strong>Database.java</strong></p>
  
  <pre><code>package com.example.user.phonebook;
  
  import android.content.ContentValues;
  import android.content.Context;
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.widget.ListView;
  
  public class Database extends SQLiteOpenHelper {
  
      public static final String DATABASE_NAME="PhoneBook.db";
      public static final String TABLE_NAME="Information";
      public static final String ID="ID";
      public static final String NAME="Name";
      public static final String EMAIL="Email";
      public static final String PHONE="Phone";
  
  
      public Database(Context context) {
          super(context, DATABASE_NAME, null, 2);
      }
  
  
  
      @Override
      public void onCreate(SQLiteDatabase db) {
  
          String create_table= "CREATE TABLE " + TABLE_NAME + "(" + ID + " TEXT,"
                  + NAME + " TEXT," + EMAIL + " TEXT," + PHONE + " TEXT" + ")";
          db.execSQL(create_table);
  
      }
  
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  
          db.execSQL("DROP TABLE IF EXISTS "+ TABLE_NAME);
          onCreate(db);
      }
  
      public boolean AddData(String id,String name, String email, String phone)
      {
          SQLiteDatabase db=this.getWritableDatabase();
          ContentValues cv= new ContentValues();
          cv.put(ID,id);
          cv.put(NAME,name);
          cv.put(EMAIL,email);
          cv.put(PHONE,phone);
  
          long result=db.insert(TABLE_NAME,null,cv);
  
          if (result==-1)
          {
              return false;
          }
          else
          {
              return true;
          }
      }
  
      public Cursor getData()
      {
  
          SQLiteDatabase db=this.getWritableDatabase();
          Cursor cv=db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
          return cv;
  
      }
  
      public boolean UpdateDate(String id, String name, String email, String phone)
      {
          SQLiteDatabase db=this.getWritableDatabase();
          ContentValues cv=new ContentValues();
          cv.put(ID,id);
          cv.put(NAME,name);
          cv.put(EMAIL,email);
          cv.put(PHONE,phone);
          db.update(TABLE_NAME,cv,"ID="+id,null);
          return true;
  
      }
      }
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>package com.example.user.phonebook;
  
  import android.database.Cursor;
  import android.database.sqlite.SQLiteDatabase;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.ListView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
      EditText ed1,ed2,ed3,ed4;
      TextView tv;
      Button bn1,bn2,bn3;
      Database mydb;
      ListView lv;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          ed1=(EditText)findViewById(R.id.editText);
          ed2=(EditText)findViewById(R.id.editText2);
          ed3=(EditText)findViewById(R.id.editText4);
          ed4=(EditText)findViewById(R.id.editText3);
          tv=(TextView) findViewById(R.id.textView2);
          bn1=(Button) findViewById(R.id.button);
          bn2=(Button) findViewById(R.id.button2);
          bn3=(Button) findViewById(R.id.button3);
          lv=(ListView)findViewById(R.id.listview) ;
  
  
          mydb=new Database(this);
  
          bn1.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  boolean value=mydb.AddData(ed4.getText().toString(),ed1.getText().toString(),ed2.getText().toString(),ed3.getText().toString());
  
                  if (value==true)
                  {
                      Toast.makeText(getApplicationContext(),"Data Inserted",Toast.LENGTH_SHORT).show();
                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(),"Error",Toast.LENGTH_SHORT).show();
                  }
              }
          });
  
  
          bn2.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Cursor cv=mydb.getData();
                  if (cv.getCount()==0)
                  {
                      Toast.makeText(getApplicationContext(),"No data found",Toast.LENGTH_SHORT).show();
                      return;
                  }
  
                  while (cv.moveToNext())
                  {
                      StringBuffer stringBuffer=new StringBuffer();
  
  
                      stringBuffer.append("ID : "+cv.getString(0)+"\n");
                      stringBuffer.append("Name : "+cv.getString(1)+"\n");
                      stringBuffer.append("Email ID : "+cv.getString(2)+"\n");
                      stringBuffer.append("Phone No. : "+cv.getString(3)+"\n\n");
  
  
                      message("Details", stringBuffer.toString());
                  }
  
  
  
              }
          });
  
  
          bn3.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  boolean isupdated=mydb.UpdateDate(ed4.getText().toString(),ed1.getText().toString(),ed2.getText().toString(),ed3.getText().toString());
                  if (isupdated==true)
                  {
                      Toast.makeText(getApplicationContext(),"Data Updated",Toast.LENGTH_SHORT).show();
                  }
                  else
                  {
                      Toast.makeText(getApplicationContext(),"Updation failed",Toast.LENGTH_SHORT).show();
                  }
  
              }
          });
  
  
  
  
  
      }
  
      public void message(String title, String messageis)
      {
          AlertDialog.Builder builder=new AlertDialog.Builder(this);
          builder.setCancelable(true);
          builder.setTitle(title);
          builder.setMessage(messageis);
          AlertDialog dialog=builder.create();
          dialog.show();
      }
  
  
  
  
  }
  </code></pre>
  
  <p><strong>XML Layout:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
  
      &lt;EditText
          android:id="@+id/editText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginTop="8dp"
          android:ems="10"
          android:hint="Name"
          android:inputType="textPersonName"
          app:layout_constraintBottom_toTopOf="@+id/editText2"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.502"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintVertical_bias="1.0" /&gt;
  
      &lt;EditText
          android:id="@+id/editText2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:ems="10"
          android:hint="Email ID"
          android:inputType="textPersonName"
          app:layout_constraintBottom_toTopOf="@+id/editText4"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.502"
          app:layout_constraintStart_toStartOf="parent" /&gt;
  
      &lt;EditText
          android:id="@+id/editText4"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="76dp"
          android:ems="10"
          android:hint="Phone Number"
          android:inputType="phone"
          app:layout_constraintBottom_toTopOf="@+id/button2"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.502"
          app:layout_constraintStart_toStartOf="parent" /&gt;
  
      &lt;Button
          android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginEnd="8dp"
          android:layout_marginStart="8dp"
          android:text="Save"
          app:layout_constraintEnd_toStartOf="@+id/button2"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="@+id/button2" /&gt;
  
      &lt;Button
          android:id="@+id/button2"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginTop="256dp"
          android:text="View"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
      &lt;Button
          android:id="@+id/button3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginEnd="8dp"
          android:layout_marginStart="8dp"
          android:text="Update"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toEndOf="@+id/button2"
          app:layout_constraintTop_toTopOf="@+id/button2" /&gt;
  
      &lt;EditText
          android:id="@+id/editText3"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_marginBottom="8dp"
          android:layout_marginTop="8dp"
          android:ems="10"
          android:hint="ID"
          android:inputType="textPersonName"
          app:layout_constraintBottom_toTopOf="@+id/button2"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.502"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/editText4"
          app:layout_constraintVertical_bias="0.076" /&gt;
  
      &lt;ListView
          android:id="@+id/listview"
          android:layout_width="347dp"
          android:layout_height="185dp"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="16dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          android:divider="#f00"
          android:dividerHeight="1sp"
          android:listSelector="#faa"
          android:orientation="vertical"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintHorizontal_bias="0.846"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toBottomOf="@+id/button2"
          app:layout_constraintVertical_bias="0.028" /&gt;
  
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>