Reputation: 1543
I'm back with another question regarding Android databases.
I'm storing a simple string into my android database and then wish to display all the entries as a list. However, the list shows nothing on my emulator (or my actual device) and neither does LogCat (either when saving entries into the DB or when calling them).
Below you have my code
the save entries function:
public long createEntry(String entry){
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ENTRY, entry);
return db.insert(DATABASE_TABLE, null, initialValues);
}
the get entries function:
public Cursor fetchAllEntries(){
return db.query(DATABASE_TABLE,new String [] {KEY_ROW_ID, KEY_ENTRY}, null, null, null, null, null);
}
the List Activity:
entryList = (ListView)findViewById(R.id.recordsList);
Cursor c = dbAdapter.fetchAllEntries();
if(c!=null){
startManagingCursor(c);
String [] from = new String [] {PirelliDbAdapter.KEY_ENTRY};
int [] to = new int [] {R.id.text1};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.entryrow, c, from, to);
entryList.setAdapter(adapter);
}
dbAdapter.close();
And finally the List Activity's XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/recordsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/textViewList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Thanks a lot for taking the time to look over this and help me out. I'm really stumped and don't know where to go from here...
Upvotes: 0
Views: 3189
Reputation: 1444
Firstly, check that your database is created or not then check whether table is created or not , through command line. If all-things are OK then try to print cursor to check it retrieves data from db or not. for printing the cursor values use following logic..
*if(!cursor.isAfterLast())
{
cursor.moveToFirst();
do{
String entry = cursor.getString(cursor.getColumnIndex(KEY_ENTRY));
Log.v("value:",entry);
cursor.moveToNext();
}while(!cursor.isAfterLast());*
Upvotes: 1
Reputation: 387
Your code has no issue , will you please check in the database file weather the data is there one way is use sqlite browser (or) go through this in command prompt
{$ adb shell $ cd data/data/projectname/databases/
check if your db file is created and then move on to -->
sqlite3 database file name you will find the sqlite prompt- hence you can explore as .tables and select * from table Name }
the above steps are mentioned in view of newbees if you know this earlier please ignore
to explore the db file.
please check this https://stackoverflow.com/questions/2149438/tool-to-see-android-database-tables-and-data
Upvotes: 0