开发者

Android getColumName and getColumnIndex

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 f开发者_JAVA技巧rom 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?


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

cursor.getColumnIndex("mainMenu.title"); // -1
cursor.getColumnIndex("title"); // 2


I had a similar problem and solved it by giving my columns aliases in my fairly complex query, then I used these aliases as references eg: The start of my query:

SELECT lith.drill_id, lith.depth_from,
...
...

cursor.getColumnIndex("depth_from") gave -1

Then I added column aliases:

SELECT lith.drill_id AS drill_id, lith.depth_from as depth_from,
...
...

cursor.getColumnIndex("depth_from") then gave the correct value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜