androniennn androniennn - 14 days ago 6
Android Question

Tapping on Map Marker results in AlertDialog displaying all other Map Markers

I have a description column in my SQLite database that contains some text to display when taping to every marker. Every marker has its own description. The problem is when I tap on one marker, I'm getting all of the other

AlertDialog
s displayed. I'm using the following code:

@Override
protected boolean onTap(int index) {
db = openHelper.getWritableDatabase();

String[] result_columns = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, result_columns, null, null, null, null, null, null);
cur.moveToFirst();
while (cur.isAfterLast() == false) {
String description = cur.getString(cur.getColumnIndexOrThrow("description"));
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();

cur.moveToNext();
}
cur.close();
db.close();

return true;
}


The
onTap
function seems to be working:

protected boolean onTap(int index) {
db = openHelper.getWritableDatabase();

String[] result_columns = new String[] {COL_DESCRI};
Cursor cur = db.query(true, TABLE_COORD, result_columns, null, null, null, null, null, null);

cur.moveToPosition(index);
String description = cur.getString(cur.getColumnIndexOrThrow("description"));

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();
cur.close();

db.close();

return true;
}


I am aware that I have to assign an index to every
AlertDialog
, but I don't know how to do it. I've tried to solve this but I can't seem to do it. Any suggestions on how to solve the problem?

Answer

You appear to be looping through all the items in your results.

cur.moveToFirst() moves you to the first item, then cur.moveToNext(); moves you to the next item on each iteration. You are then displaying the alert dialog for every item!

You really shouldn't need to load the data like this every time the user clicks on an item, but the simplest [not recommended] way to fix your code would be to use cur.moveToPosition(index) instead of your loop.

Comments