title: Creating a working ListView activity with information retrieved from a SQLite Database
body: <p>I am working on a project which requires me to display a list of countries in a listview on the first activity, and when any of them are pressed, it opens a new activity with its corresponding data showing accordingly. I am trying to implement this using the SQLite database.</p>
  
  <p>I am not sure what is wrong with my code, i have been working on it for hours and still can not seem to locate the problem.</p>
  
  <p>This is my MainActivity:</p>
  
  <p>public class MainActivity extends ListActivity {</p>
  
  <pre><code> private DatabaseHelper dbHelper;
   private SimpleCursorAdapter dataAdapter;
  
   @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  
    dbHelper = new DatabaseHelper(this);
    dbHelper.open();
  
    //Generate ListView from SQLite Database
    displayListView();
   }
  
  
   String [] countries = new String []{
           DatabaseHelper.COUNTRY
   };
  
   private void displayListView() {
  
  
    Cursor cursor = dbHelper.fetchAllCountries();
  
    // The desired columns to be bound
    String[] columns = new String[] {
              DatabaseHelper.COUNTRY,
              DatabaseHelper.CAPITAL,
              DatabaseHelper.CURRENCY,
              DatabaseHelper.POPULATION
            };
  
  
    // the XML defined views which the data will be bound to
    int[] to = new int[] { 
              R.id.text
    };
  
    // create the adapter using the cursor pointing to the desired data 
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
      this, R.layout.country_info, 
      cursor, 
      countries, 
      to,
      0);
  
    ListView listView = (ListView) findViewById(R.id.label);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);
  
  
    listView.setOnItemClickListener(new OnItemClickListener() {
     @Override
     public void onItemClick(AdapterView&lt;?&gt; listView, View view, 
       int position, long id) {
     // Get the cursor, positioned to the corresponding row in the result set
     Cursor cursor = (Cursor) listView.getItemAtPosition(position);
  </code></pre>
  
  <p>and this is my DatabaseHelper: </p>
  
  <pre><code>private SQLiteDatabase mydb;
  private static final String DATABASE_NAME="countries_db";
  static final int DATABASE_VERSION = 1;
  private static final String KEY_ID = "id";
  static final String COUNTRY="country";
  static final String CAPITAL="capital";
  static final String CURRENCY="currency";
  static final String POPULATION="population";
  private static final String SQLITE_TABLE = "countries";
  
  public DatabaseHelper(Context context) {
  super(context, DATABASE_NAME, null, DATABASE_VERSION);
  
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
  String CREATE_CONTACTS_TABLE = "CREATE TABLE " + SQLITE_TABLE + "(" + KEY_ID + "
  INTEGER PRIMARY KEY," + COUNTRY + " TEXT," + CAPITAL + " TEXT" + CURRENCY 
  + " TEXT," + POPULATION + " REAL" + ")";
  db.execSQL(CREATE_CONTACTS_TABLE);
  
  //this is populating the database
  ContentValues cv=new ContentValues();
  cv.put(COUNTRY, "EGYPT");
  cv.put(CAPITAL, "CAIRO");
  cv.put(CURRENCY, "EGP");
  cv.put(POPULATION, "90");
  db.insert("countries", null, cv);
  
  cv.put(COUNTRY, "Jordan");
  cv.put(CAPITAL, "Amman");
  cv.put(CURRENCY, "Jordanian Dinar");
  cv.put(POPULATION, "6.3");
  db.insert("countries", null, cv);
  
  cv.put(COUNTRY, "Kuwait");
  cv.put(CAPITAL, "Kuwait City");
  cv.put(CURRENCY, "Kuwait Dinar");
  cv.put(POPULATION, "3.25");
  db.insert("countries", null, cv);
  
  cv.put(COUNTRY, "Saudi Arabia");
  cv.put(CAPITAL, "Riyadh");
  cv.put(CURRENCY, "Riyal");
  cv.put(POPULATION, "29");
  db.insert("countries", null, cv);
  }
  
  // Upgrading database
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
  // Drop older table if existed
  db.execSQL("DROP TABLE IF EXISTS " + SQLITE_TABLE);
  onCreate(db);
  }
  
  public void open() {
  // TODO Auto-generated method stub
  
  }
  public Cursor fetchAllCountries() {
  SQLiteDatabase mydb = this.getWritableDatabase();
  
    Cursor cursor = mydb.query(SQLITE_TABLE, new String[] {COUNTRY, CAPITAL, CURRENCY,
   POPULATION}, 
      null, null, null, null, null);
  
    if (cursor != null) {
     cursor.moveToFirst();
    }
    return cursor;
   }
  }
  </code></pre>