【问题标题】:Any alternative to this code?此代码的任何替代方法?
【发布时间】:2011-08-24 16:28:37
【问题描述】:

我已经尝试了很多方法来使这段代码正常工作,它包括在点击地图上的元素/标记时显示 AlertDialog/描述。问题是 4 个标记中的 3 个有效(但没有显示正确的描述。它看起来像前一个)并且最后一个强制关闭应用程序并出现 logcat 错误:

CursorIndexOutOfBoundsException: Index 4 requested, with a size of 4

代码:

@Override
        protected boolean onTap(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);

            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();

             cur.close();
             db.close();

            return true;
        }

【问题讨论】:

    标签: android sqlite google-maps google-maps-markers


    【解决方案1】:

    您不应该直接使用索引,相反,您可能有一个适配器可以为您提供要显示的对象或行的 _id,您可以使用它来伪造精确的请求

    编辑

    您正在使用 ItemizedOverlay。因此,您可以通过以下方式获取点击的项目:

    public void onTap(int index) {
        Item item = getItem(index);
    }
    

    那么我想你可以从项目而不是从光标中获取相关信息。

    【讨论】:

    • 我真的不明白你的回答。你能发布一个示例代码吗?
    • 对于输出“描述”列的游标查询,该列将包含 AlertDialogs 的内容?我删除它?以及如何指示 AlertDialog 必须指向该元素并显示其描述?
    • Item 类是您自己的,我假设,因此您可能知道其中是否有描述,或引用数据库中行的 id。
    【解决方案2】:

    只需使用cur.moveToPosition(index - 1);

    【讨论】:

    • 成功了,但同样的问题,导致 FC 的标记只是切换到另一个。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-02
    • 2022-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多