0

There are no errors in my code. It works just fine except that the Spinner isn't populated from the database as expected. Rather, it is empty. Please help!!

Retrieve Records from SQLite:

// get all contacts 
public List getAllNames() { 
List names = new ArrayList();

// Select All Query
String selectQuery = "SELECT  * FROM " + TABLE_NAME;

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
    do {
        names.add(cursor.getString(1));
    } while (cursor.moveToNext());
}

// closing connection
db.close();

// returning contacts
return names;

}

Load Spinner:

private void loadSpinnerData() { 
// database handler 
DatabaseHandler db = new DatabaseHandler(getApplicationContext());

// Spinner Drop down elements
List<String> contacts = db.getAllNames();

// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_spinner_item, contacts);

// Drop down layout style - list view with radio button
dataAdapter
        .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);

}

I guess all it needs is a minor tweak somewhere, but I don't understand where. Thanks in advance!

  • 1
    Are you sure you are using the right column index? and that your database is populated? – Gomino Jul 22 '13 at 00:51
  • 2
    Have u checked that getAllNames() is not returning empty set? – Pulkit Sethi Jul 22 '13 at 00:52
  • I just found out that the method returns empty set. Does that mean my database isn't populated?? I created a database using SQLite and saved it in assets folder. Am I going wrong?? – Srihari Srinivasan Jul 22 '13 at 01:22
  • please print contacts.size before setting adapter.and give response so i can give you idea. – Harshid Jul 22 '13 at 06:03
  • I set contacts.size to a textview and it always stays zero. So, the problem is at reading the database. I have xyz.db and xyz.sqlite in assets folder. Still no use. – Srihari Srinivasan Jul 22 '13 at 19:51
  • Maybe the DatabaseHandler can't find the database in the assets folder? Assuming you have a database with a table that has entries and the constant TABLE_NAME has the correct table name, try this out http://stackoverflow.com/questions/9109438/how-to-use-an-existing-database-with-an-android-application – fgharo91 Jun 20 '15 at 04:01

0 Answers0