I have this code to show an AlertDialog when taping on a marker:
public void showOverlay (OverlayItem overlay)
{
db = openHelper.getWritableDatabase();
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
dialog.setTitle("Infos.");
dialog.setMessage(description);
dialog.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
db.close();
}
@Override
public boolean onTap(int index)
{
showOverlay(getItem(index)) ;
return super.onTap(index) ;
}
The strange thing that i have a FC with a logcat error:
08-24 20:51:42.466: ERROR/AndroidRuntime(265): Uncaught handler: thread main exiting 开发者_JS百科due to uncaught exception
08-24 20:51:42.486: ERROR/AndroidRuntime(265): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 4
Any idea please? Thank you.
When the cursor is first created, it's index is at -1 (before the beginning of the list). You need to call cur.moveToFirst()
before you attempt to retrieve any data from it. moveToFirst()
will also return false if the cursor is empty, which could be useful:
public void showOverlay (OverlayItem overlay, int index)
{
db = openHelper.getWritableDatabase();
String[] columns_descri = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, columns_descri,null, null, null, null, null, null);
if (cur.moveToPosition(index)) {
String description = cur.getString(cur.getColumnIndexOrThrow(COL_DESCRI));
AlertDialog.Builder dialog = new AlertDialog.Builder(Geo.this);
dialog.setTitle("Infos.");
dialog.setMessage(description);
dialog.setPositiveButton("OK", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
else {
//ERROR! cursor is empty, throw a toast or something
}
db.close();
}
精彩评论