Android getColumName and getColumnIndex

I am a beginner android and I have a problem with my cursor. I can not access the data using: cursor.get(cursor.getColumnIndex(columnName));

I tried the following code to check for errors:

 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 

Should the result "Index Again" be 2? What am I doing wrong?

+6
android sqlite
source share
2 answers

cursor.getColumnIndex() expects a column name without a table name:

 cursor.getColumnIndex("mainMenu.title"); // -1 cursor.getColumnIndex("title"); // 2 
+3
source share

I had a similar problem, and I solved it by specifying my column aliases in my rather complex query, then I used these aliases as links, for example: Beginning of my query:

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

cursor.getColumnIndex("depth_from") gave -1

Then I added the 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
source share

All Articles