title: How to Read Data from SQLite Database and show it on a list view
body: <p>How to pull Data from SQLite Database and show it on a list view.</p>
  
  <p>I am doing this in my code, but I am getting an error:</p>
  
  <pre><code>public class ShoewDataListActivity extends ListActivity  {
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.grupadd);
          Button btnAdd = (Button) findViewById(R.id.btnAdd);
          Button view=(Button) findViewById(R.id.btnShowData);
          view.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
                  displayResultList();
              }
          });
          btnAdd.setOnClickListener(new OnClickListener() {
  
              public void onClick(View v) {
  
                  boolean didItWork = true;
  
                  try {
                      EditText edtAddName=(EditText) findViewById(R.id.editNameAdd);
                      String name = edtAddName.getText().toString();
                      AddData entry = new AddData(ShoewDataListActivity.this);
                      entry.open();
                      entry.createEntry(name);
                      entry.close();
                  } catch (Exception e) {
                      // e.printStackTrace();
                      didItWork = false;
                      String error = e.toString();
                      Dialog d = new Dialog(ShoewDataListActivity.this);
                      d.setTitle("What happend...!!");
                      TextView tv = new TextView(ShoewDataListActivity.this);
                      tv.setText("...." + error + "....");
                      d.setContentView(tv);
                      d.show();
  
                  } finally {
                      if (didItWork) {
                          // Dialog d = new Dialog(GroupActivity.this);
                          // d.setTitle("This is ok..........!!");
                          // TextView tv= new TextView(GroupActivity.this);
                          // tv.setText("....SuccessFull....");
                          // d.setContentView(tv);
                          // d.show();
                          Toast.makeText(getBaseContext(), "Project Saved",
                                  Toast.LENGTH_LONG).show();
  
  
  
                      }
                  }
              }
          });
  
      }
  
      private void displayResultList() {
          TextView tView = new TextView(this);
          AddData info = new AddData(this);
          ArrayList&lt;String&gt; results = new ArrayList&lt;String&gt;();
          results = info.fatchData();
          tView.setText("This data is retrieved from the database and only 4 "
                  + "of the results are displayed");
          getListView().addHeaderView(tView);
          setListAdapter(new ArrayAdapter&lt;String&gt;(this,
                  android.R.layout.simple_list_item_1, results));
          getListView().setTextFilterEnabled(true);
  
      }
  
  
  }
  </code></pre>
  
  <p>And my Dbhelper class is:</p>
  
  <pre><code>public class AddData {
  
      public static final String TAG = DbHelper.class.getSimpleName();
      public static final String DB_NAME = "Grup.db";
      public static final int DB_VERSION = 1;
      public static final String TABLE = "Grups";
      public static final String C_ID = BaseColumns._ID;
      public static final String C_CREATED_AT = "easy_ass_created_At";
      public static final String C_NAME = "name";
      private DbHelper ourHelper;
      private final Context ourContext;
      private SQLiteDatabase ourDatabase;
  
      public class DbHelper extends SQLiteOpenHelper {
  
          // public static final String TAG = DbHelper.class.getSimpleName();
          // public static final String DB_NAME = "Grup.db";
          // public static final int DB_VERSION = 1;
          // public static final String TABLE = "Grups";
          // public static final String C_ID = BaseColumns._ID;
          // public static final String C_CREATED_AT = "easy_ass_created_At";
          // public static final String C_NAME = "name";
  
          public DbHelper(Context context) {
              super(context, DB_NAME, null, DB_VERSION);
  
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) {
  
              String sql = ("create table " + TABLE + " ( " + C_ID
                      + " integer primary key autoincrement, " + C_NAME
                      + " text not null" + ");");
              db.execSQL(sql);
              Log.d(TAG, "OnCreate sql" + sql);
  
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
              db.execSQL("drop table if exists " + TABLE);
              this.onCreate(db);
              Log.d("TAG", "********On Upgrate Drop Table*****");
  
          }
  
      }
  
      public AddData(Context context) {
          ourContext = context;
  
      }
  
      public AddData open() throws SQLException {
  
          ourHelper = new DbHelper(ourContext);
          ourDatabase = ourHelper.getReadableDatabase();
          return this;
      }
  
      public void close() {
          ourHelper.close();
      }
  
      public long createEntry(String name) {
          ContentValues cv = new ContentValues();
          cv.put(C_NAME, name);
          return ourDatabase.insert(TABLE, null, cv);
      }
  
      public String getData() {
  
          String[] columns = new String[] { C_ID, C_NAME };
          Cursor c = ourDatabase.query(TABLE, columns, null, null, null, null,
                  null);
          String result = "";
          // int iRow=c.getColumnIndex(C_ID);
          int iName = c.getColumnIndex(C_NAME);
  
          // for(c.moveToFirst();!c.isAfterLast();c.moveToLast()){
          for (boolean hasItem = c.moveToFirst(); hasItem; hasItem = c
                  .moveToNext()) {
              result = result + "   " + c.getString(iName) + "\n";
              c.moveToNext();
          }
          c.close();
          return result;
  
      }
  
      public ArrayList&lt;String&gt; fatchData(){
  
          String[] columns = new String[] { C_ID, C_NAME };
          Cursor c = ourDatabase.query(TABLE, columns, null, null, null, null,
                  null);
          String result="";
          ArrayList&lt;String&gt; result1=new ArrayList&lt;String&gt;();
          // int iRow=c.getColumnIndex(C_ID);
          int iName = c.getColumnIndex(C_NAME);
  
          // for(c.moveToFirst();!c.isAfterLast();c.moveToLast()){
          for (boolean hasItem = c.moveToFirst(); hasItem; hasItem = c
                  .moveToNext()) {
  
              result = "   " + c.getString(iName) + "\n";
              result1.add(result);
              c.moveToNext();
          }
          c.close();
          return result1;
  
  
      }
  }
  </code></pre>