I am trying to get information from my database and put it into a string to print it on screen. i thought i could use the code below to do it but it gives out some information about the cursor instead of the information inside the cursor.
datasource = new DataBaseHelper(this);
datasource.open();
Cursor c = datasource.getAllGoals();
startManagingCursor(c);
String g = c.toString();
goal.setText(g);
datasource.close();
The cursor can be thought of as a pointer to some underlying data. Running
c.toString()on the cursor object would print the defaultCursorclass’ implementation of its string representation (an at-sign character@and the unsigned hexadecimal representation of the hash code of the object) which is not what you want.To retrieve underlying database data, you will need to call
c.getString(columnIndex)(source), or whatever column datatype you require for that particular column index.Here’s an example modified from source:
Say you have created a table
and your
getAllGoalsfunction returns a cursor which points to data about both_idandcomment. Now you only want to show the details about thecommentcolumn. So you have to runc.getString(1). Suppose yourgetAllGoalsfunction returns a cursor which only points to data about thecommentcolumn. Now you have to runc.getString(0).I would recommend that you download the source code in the provided example and go through how data is retrieved from a cursor.
EDIT:
source