0

Basically, I want to return the actual value in the database and not the position that is currently being pointed to. From reading up on this, the getString method seems to be the guy that I need, but that doesn't seem to be working. Here's the code:

public Cursor fetchNote(long rowId) throws SQLException {

    Cursor mCursor =

        mDb.query(true, DATABASE_TABLE, new String[] {KEY_TITLE}, KEY_ROWID + "=" + rowId, null,
                null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();

    }

    return mCursor;

So that's my pretty standard query, which should grab a result for me (i.e. the position of my mCursor). Now say that value is 'Fred' I want to show that on my Application. This is the code I have in my activity:

TextView currentName = (TextView) findViewById(R.id.currentName);
        String me = myDatabaseHelper.fetchNote(1).toString();
        currentName.setText(me);

Now, when I run this and click on the button that has a listener for this, it shows the following android.database.sqlite.SQLiteCursor@44f78c00 as I kind of expect, but I just can't get it to show me the darn value. I think I need to incorporate mCursor.getString(mCursor.getColumnIndex("title")); or something of the sort in my query, but it's returning the mCursor value at the moment, is there a separate method I should build to return the index? Sorry if that is shadily explained, I'm just in a bit of a muddle!

If you need more code/more info let me know, thank you in advance to whoever helps me with this one.

Dan J
  • 16,319
  • 7
  • 50
  • 82
Creights
  • 907
  • 1
  • 12
  • 25

2 Answers2

0

The cursor is the whole data structure with your information. Assuming you have executed the query and have the cursor loaded, you need to use one of the cursors functions to output the current row.

Running a toString() on the entire cursor is the output you are seeing.

On the examples I see online, I don't see them saying return mCursor... they just say return;

Also try the following:

mCursor.getString(mCursor.getColumnIndex("title"));
mvrak
  • 501
  • 3
  • 12
  • Thanks for the reply. See this code: KEY_ROWID + "=" + rowId This is where I'm defining what row I am using (higher up in the code KEY_ROWID is a constant that I'm using.) In this example, I don't want to output the entire row, but one item from a particular column in a particular row. I understand what you saying in terms of outputting the object location and not the actual value, but what do I need to do to get the value? Thanks again for your help. – Creights Jan 23 '12 at 22:41
0
mCursor.getString("title");

should return title in cursor.

If you want Index, mCursor.getColumnIndex("title") should be enough.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • Hey, I tried to apply mCursor.getString("title"); but it tells me that it needs to be a getLong. As the method is expecting a String I'm not sure how to implement it. I also tried the mCursor.getColumnIndex("title") but that gave me the same result (android.database.sqlite.SQLiteCursor@44f78c00). The above answer has helped me understand why it is printing that, but I still don't see how I can get this value to be shown. If you can help me further I'd really appreciate it! – Creights Jan 23 '12 at 22:44
  • You want to get the title (or) index of title (index of title means, let us say your cursor has "Title1" in response of the query, index you get will be 0 which is long). I am not clear on what you are looking for. – kosa Jan 23 '12 at 22:48
  • Ahh sorry for the confusion. Say the entry in the database is Fred at position 0 I want it to give me the value 'Fred'. I'm not concerned with printing the location. In terms of updating the question with the latest code, I haven't made any changes to the method above. I'm calling that fetchNote method from another class (this fetchNote method is within my DBHelper class) and hoping to get the value (such as Fred) in return. I hope that's clearer. If you want to see each class in their entirety I can post those, I'm just not sure if there are enough characters allowed to do so! Thank you. – Creights Jan 24 '12 at 09:05