title: how display data of database in listview on android
body: <p>I want to display data of a database in a listView but my project has a problem.
  Where in my code is the problem?
  Here is my code:</p>
  
  <pre><code>public class Activity2 extends ListActivity {
          DBAdapter db = new DBAdapter(this); 
  
  
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              //setContentView(R.layout.activity_activity2);
              db.open();
              Cursor c = db.getAllContacts();
              if (c.moveToFirst())
              {
                  do {          
                      DisplayContact(c);
                  } while (c.moveToNext());
              }
              db.close();
          }
  
      public void DisplayContact(Cursor c)
      {
  
          ListView lstView = getListView();
          //lstView.setChoiceMode(0); //CHOICE_MODE_NONE
          //lstView.setChoiceMode(1); //CHOICE_MODE_SINGLE
          lstView.setChoiceMode(2);   //CHOICE_MODE_MULTIPLE
          lstView.setTextFilterEnabled(true);
  
         // presidents = getResources().getStringArray(R.array.presidents_array);
  
          setListAdapter(new ArrayAdapter&lt;DBAdapter&gt;(this,
                  android.R.layout.activity_list_item));
      }
      }
  </code></pre>
  
  <p>** </p>
  
  <p>And here is my database:</p>
  
  <pre><code>public class DBAdapter {
      public static final String KEY_ROWID = "_id";
      public static final String KEY_NAME = "name";
      public static final String KEY_PRICE = "price";
      private static final String TAG = "DBAdapter";
  
      private static final String DATABASE_NAME = "MyDB";
      private static final String DATABASE_TABLE = "contacts";
      private static final int DATABASE_VERSION = 2;
  
      private static final String DATABASE_CREATE =
          "create table contacts (_id integer primary key autoincrement, "
          + "name text not null, price text not null);";
  
      private final Context context;    
  
      private DatabaseHelper DBHelper;
      private SQLiteDatabase db;
  
      public DBAdapter(Context ctx) 
      {
          this.context = ctx;
          DBHelper = new DatabaseHelper(context);
      }
  
      private static class DatabaseHelper extends SQLiteOpenHelper 
      {
          DatabaseHelper(Context context) 
          {
              super(context, DATABASE_NAME, null, DATABASE_VERSION);
          }
  
          @Override
          public void onCreate(SQLiteDatabase db) 
          {
              try {
                  db.execSQL(DATABASE_CREATE);    
              } catch (SQLException e) {
                  e.printStackTrace();
              }
          }
  
          @Override
          public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
          {
              Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                      + newVersion + ", which will destroy all old data");
              db.execSQL("DROP TABLE IF EXISTS contacts");
              onCreate(db);
          }
      }    
  
      //---opens the database---
      public DBAdapter open() throws SQLException 
      {
          db = DBHelper.getWritableDatabase();
          return this;
      }
  
      //---closes the database---    
      public void close() 
      {
          DBHelper.close();
      }
  
      //---insert a contact into the database---
      public long insertContact(String name, String price) 
      {
          ContentValues initialValues = new ContentValues();
          initialValues.put(KEY_NAME, name);
          initialValues.put(KEY_PRICE, price);
          return db.insert(DATABASE_TABLE, null, initialValues);
      }
  
      //---deletes a particular contact---
      public boolean deleteContact(long rowId) 
      {
          return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) &gt; 0;
      }
  
      //---retrieves all the contacts---
      public Cursor getAllContacts() 
      {
          return db.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_NAME,
                  KEY_PRICE}, null, null, null, null, null);
      }
  
      //---retrieves a particular contact---
      public Cursor getContact(long rowId) throws SQLException 
      {
          Cursor mCursor =
                  db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                  KEY_NAME, KEY_PRICE}, KEY_ROWID + "=" + rowId, null,
                  null, null, null, null);
          if (mCursor != null) {
              mCursor.moveToFirst();
          }
          return mCursor;
      }
  
      //---updates a contact---
      public boolean updateContact(long rowId, String name, String price) 
      {
          ContentValues args = new ContentValues();
          args.put(KEY_NAME, name);
          args.put(KEY_PRICE, price);
          return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) &gt; 0;
      }
  }
  </code></pre>