Cursor IndexOutBounsException

问题描述 投票:-1回答:1

我编写了这段代码,但这会在游标中启动异常。

SQLiteDatabase db=a.getReadableDatabase();
String[] nombre= {ed_nombre.getText().toString()};
String name[]= {nombre+"%"};
String[] campos={"nombre,telefono"};

try {

    Cursor cursor = null;
    cursor=db.query("contactos", campos,"nombre like ?", name , null, null, null);
    }

        catch (Exception e){...}

我的bd名称是“ contactos”,而我的ed_nombre是正确的。为什么会遇到此异常?

sqlite android-studio android-sqlite android-cursoradapter
1个回答
-1
投票

变量nombre应该是String,而不是字符串数组:

String nombre = ed_nombre.getText().toString();

此字符串将作为唯一项放入数组name中:

String name[]= { nombre + "%" };

并且此数组作为query()的第4个参数传递。还将您的代码简化为:

Cursor cursor = db.query("contactos", campos,"nombre like ?", name , null, null, null);

您不需要2个单独的语句。

© www.soinside.com 2019 - 2024. All rights reserved.