点击地图标记会导致 AlertDialog 显示所有其他地图标记
我的 SQLite 数据库中有一个描述列,其中包含一些在点击每个标记时要显示的文本。每个标记都有自己的描述。问题是,当我点击一个标记时,我会显示所有其他 AlertDialog
。我正在使用以下代码:
@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;
}
onTap
函数似乎正在工作:
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;
}
我知道我必须为每个 AlertDialog
分配一个索引,但我不知道怎么做。我试图解决这个问题,但我似乎做不到。关于如何解决问题有什么建议吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您似乎正在循环遍历结果中的所有项目。
cur.moveToFirst() 将您移至第一项,然后 cur.moveToNext(); 在每次迭代中将您移至下一项。然后您将显示每个项目的警报对话框!
您确实不需要每次用户单击某个项目时都加载这样的数据,但修复代码的最简单的[不推荐]方法是使用 cur.moveToPosition(index)而不是你的循环。
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.