【问题标题】:Tapping on Map Marker results in AlertDialog displaying all other Map Markers点击地图标记会在 AlertDialog 中显示所有其他地图标记
【发布时间】:2011-11-02 01:59:26
【问题描述】:

我的 SQLite 数据库中有一个描述列,其中包含一些要在点击每个标记时显示的文本。每个标记都有自己的描述。问题是当我点击一个标记时,我会显示所有其他 AlertDialogs。我正在使用以下代码:

@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 分配一个索引,但我不知道该怎么做。我试图解决这个问题,但我似乎做不到。有关如何解决问题的任何建议?

【问题讨论】:

    标签: android sqlite android-sqlite


    【解决方案1】:

    您似乎正在遍历结果中的所有项目。

    cur.moveToFirst() 将您移至第一项,然后 cur.moveToNext(); 将您移至每次迭代的下一项。然后,您将显示每个项目的警报对话框!

    你真的不应该在每次用户点击一个项目时都加载这样的数据,但是修复你的代码最简单的[不推荐]方法是使用 cur.moveToPosition(index) 而不是你的循环。

    【讨论】:

    • 没错。你的意思是我必须用cur.moveToPosition(index)替换cur.moveToFirst()并删除cur.moveToNext()?为什么不推荐呢?
    • 不推荐,因为您应该只加载一次数据 - 当您将其绑定到视图时。您尚未发布此代码,因此很难发表评论。您应该将其加载到本地集合中。然后,当用户点击一个项目时,只需通过其索引访问正确的项目。
    • 我试过cur.moveToPosition(index),应用程序冻结然后强制关闭。
    • 您没有发布新代码,但我只能猜测您只是删除了该行,而不是试图了解正在发生的事情。因此,您处于无限循环中。摆脱你的while循环。你有没有想过如果在显示数据和点击一个项目之间删除一个项目会发生什么?
    • 是的,你有理由,它工作在 90%。我只是有一个带有索引 4 标记的 FC:08-23 16:40:08.491: ERROR/AndroidRuntime(6742): android.database.CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4 编译器必须为这个标记请求索引 3?你认为那个 FC 的来源是什么?
    猜你喜欢
    • 2018-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-04
    • 2010-11-06
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    相关资源
    最近更新 更多