Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android getColumName and getColumnIndex

Tags:

android

sqlite

I'm an android novice and I have a problem with my cursor. I can't access the data using: cursor.get(cursor.getColumnIndex(columnName));

I tried the following code to test for an error:

while (cursor.moveToNext()) {
        int x = 2;
        Log.i("MyDebug", "Index: " + x);
        Log.i("MyDebug", "Name: " + cursor.getColumnName(x));
        Log.i("MyDebug", "Index again: " + cursor.getColumnIndex(cursor.getColumnName(x)));
    }

Result from the Debug Monitor:

Index: 2 
Name: mainMenu.title 
Index again: -1

Shouldn't the result of "Index again" be 2? What am I doing wrong?

like image 303
Mokus Avatar asked Dec 22 '10 17:12

Mokus


1 Answers

cursor.getColumnIndex() expects the name of the column, without the name of the table:

cursor.getColumnIndex("mainMenu.title"); // -1
cursor.getColumnIndex("title"); // 2
like image 67
Cristian Avatar answered Sep 27 '22 17:09

Cristian