Question

I don't think I've been doing this right. A buddy told me to use the cursor.getColumnIndex() to retrieve the values of the items in that table column. Looks to me like it just returns an int value of the position of the column in the table which is why when I execute the code below I get either a 2.0 or 3.0 for my lat and long values as they are the second and third column in the table.

What is the correct way to have this method return the items in the lat long columns instead of their position in the table?

Here is what I was using with the getColumnIndex() :

public List<LatLng> getAllPositions() {
     List<LatLng> positionList = new ArrayList<LatLng>();
      SQLiteDatabase database;
      ResumeMySQLiteHelper dbHelper;

      dbHelper = new ResumeMySQLiteHelper(this);
     database = dbHelper.getWritableDatabase();

     String[] allColumns = { ResumeMySQLiteHelper.COLUMN_ID,
              ResumeMySQLiteHelper.COLUMN_COMMENT, ResumeMySQLiteHelper.COLUMN_LAT, ResumeMySQLiteHelper.COLUMN_LONG };

     Cursor cursor = database.query(ResumeMySQLiteHelper.TABLE_NAME,
         allColumns, null, null, null, null, null);

     cursor.moveToFirst();
     while (!cursor.isAfterLast()) { 


         double latitude = cursor.getColumnIndex("lat");
         double longitude = cursor.getColumnIndex("long");
         String testString = Double.toString(latitude);
         String testLong = Double.toString(longitude);
         Log.w("myApp", testString + " " + testLong);

         //currently prints myApp 2.0 3.0 for every row in table

         LatLng newLatLng = new LatLng(latitude, longitude);
         positionList.add(newLatLng);
         cursor.moveToNext();
     }
     // make sure to close the cursor
     cursor.close();
     return positionList;
     }
Was it helpful?

Solution

This:

double latitude = cursor.getColumnIndex("lat");

should be:

double latitude = cursor.getDouble(cursor.getColumnIndex("lat"));

and the same for double longitude

OTHER TIPS

int cloumnIndex = cursor.getColumnIndex("lat"); provides you index of the column that holds "lat" value after finding the index of that column you need to find value.

cursor.getDouble(columnIndex);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top