Getting Strings from A Cursor in Android?
25,284
Solution 1
Use this:
if (cursor.moveToFirst()) {
str = cursor.getString(cursor.getColumnIndex("content"));
}
Solution 2
First you'll want to move the Cursor you got from your Query to the first row using the moveToFirst()
-method.
After that you can get your fields values by using the getString()
or getInt()
-methods.
![iTurki](https://i.stack.imgur.com/Vszxr.jpg?s=256&g=1)
Author by
iTurki
Updated on July 15, 2020Comments
-
iTurki almost 4 years
My cose is SIMPLE. I have a SQLite-Database with three columns:
- rowid
- title => text
- content => text
This is a method in my
dataBase
-class:public Cursor fetchMessage(String tableNane, int id) { return myDataBase.rawQuery("SELECT rowid as _id, title FROM "+tableName +" WHERE rowid = "+id, null); }
This method will return only one row.
In my activity I wrote this:
Cursor cursor = myDbHelper.fetchMessage(tableName, id);
Now, I want to store the content field's text in a String.
How can this be done?
-
iTurki almost 13 yearsWorks perfectly. Thanks @CeKup :)