Android SQLite列值不存在问题



在某些连线问题上需要帮助。我有一个sqlite表项目如下

item_id    item_name
1          first
2          second
3          third

等等

这段代码将itemid和itemname完美地插入到数组中。但当我通过异常将其分配给CustomCursorAdapter时

"列‘first’不存在"

请帮帮我,我是安卓系统的新手。

        c = db.rawQuery("select item_id as _id, item_name from items", null);
        int i = 0;
        c.moveToFirst();
        do {
            from[i] = c.getString(1);
            to[i] = c.getInt(0);
            Log.i("item ", to[i] + " " + from[i]);
            i++;
        } while (c.moveToNext());
        try {
            CustomCursorAdapter ca = new CustomCursorAdapter(this,
                    android.R.layout.simple_list_item_1, c, from, to);
            getListView().setAdapter(ca);
        } catch (Exception e) {
            Log.e("Ex ", e.getMessage()); // exception : column 'first' does not exists.
        }

这是因为您正在设置from[0] = c.getString(1)

getString()是基于零的,因此在您的情况下返回"first"。然后,您将from作为类的from参数传递(我猜它扩展了SimpleCursorAdapter),然后在适配器尝试映射此列名时收到此错误。在这种情况下,from表示保存要绑定到UI的数据的列名列表,由to表示。

来自中的参数

CustomCursorAdapter ca = new CustomCursorAdapter(this,
                    android.R.layout.simple_list_item_1, c, from, to);

需要列名作为参数,而您将列值array传递给from array,而"to"需要是要取消投影的布局的文本视图的id,因此在本例中id是android。R.id.textview1,因此执行以下操作:

String[] from= new String[]{"item_name"};
int[] to= new int[]{android.R.id.textView1};

然后对这些参数执行查询。

最新更新