//function which is in my DatabaseHelper class which extends SQLiteOpenHelper
public String getCoordinatesLatitude(int id) {
String rowLat = "";
SQLiteDatabase db = this.getReadableDatabase();
String latQuery = "SELECT " + KEY_LATITUDE + "FROM " + TABLE_COORDINATES + "WHERE " + KEY_ID + "=" + id;
Cursor cursorr = db.rawQuery(latQuery,null);
if (cursorr != null){
cursorr.moveToPosition(id);
rowLat = cursorr.getString(cursorr.getColumnIndex(KEY_LATITUDE));
}
// return coordinates
return rowLat;
}
//function in my main activity
public void onListItemClick(ListView l, View v, int position, long id) {
selectedFromList = (String) (l.getItemAtPosition(position));
selectedItem = (int) l.getItemIdAtPosition(position);
String rowLat = helper.getCoordinatesLatitude(selectedItem);
Toast.makeText(getApplicationContext(), rowLat, Toast.LENGTH_SHORT).show();
}
I have a listview
which contains locations
. Each location contains latitude
,longitude
and a date. What i basically want is to click on a listview
item, and retrieve the items latitude
value from my database, based on its listview
ID. I setup the getCoordinatesLatitude()
function but i dont know if my code is correct, because when i click on a listview
item i get force close and logcat shows nullPointerException
. How can i make this work? Thanks
What my listview looks like
UPDATE!!!! I managed to get rid of nullPointerException error, and changed my code to this
public String getLatitudeFromId(long id) {
String rowLat = "not found";
SQLiteDatabase db = this.getReadableDatabase();
//String latQuery = "SELECT " + KEY_LATITUDE + " FROM " + TABLE_COORDINATES + " WHERE " + KEY_ID + "=" + id;
//Cursor cursor = db.rawQuery(latQuery,null);
Cursor cursor = db.query(TABLE_COORDINATES, new String[] { "latitude" },"id="+id, null, null, null,null);
if (cursor.moveToFirst()){
cursor.moveToPosition((int) id);
rowLat = cursor.getString(cursor.getColumnIndex("latitude"));
}
cursor.close();
db.close();
// return coordinates
return rowLat;
}
and
public void onListItemClick(ListView l, View v, int position, long id) {
DatabaseHelper helper = new DatabaseHelper(this);
selectedFromList = (String) (l.getItemAtPosition(position));
selectedItem = l.getItemIdAtPosition(position);
String rowLat = helper.getLatitudeFromId(selectedItem);
Toast.makeText(getApplicationContext(), rowLat, Toast.LENGTH_SHORT).show();
}
But now, when i click on listview item, the toast is not found, so it stops in the if (cursor.moveToFirst()) statement because the cursor is empty. How can the cursor be empty when my listview is full of items? :P
UPDATE2 I fixed the problem just by changing my query to this Cursor cursor = db.query(TABLE_COORDINATES, new String[] {KEY_DATE}, null, null, null, null,null); where KEY_DATE is the column name that you want to be shown when u click an item in the listview
i actually worked my way out. Strangely though, i changed my query to this Cursor cursor = db.query(TABLE_COORDINATES, new String[] {KEY_DATE}, null, null, null, null,null); just letting the selection to null and only having the column that i wanted, and it worked! This question can be marked as answered.