2012-03-13 31 views
0

需要帮助解决一些有线问题。我有一个SQLite表的项目,如以下Android SQLite列值不存在问题

item_id item_name 
1   first 
2   second 
3   third 

这段代码被插入ITEM_ID和ITEM_NAME到阵列完全正常。但是,当我将其指定为通过异常CustomCursorAdapter它

“列‘第一’并不存在”

,请帮助我是新手到Android。

 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. 
     } 

回答

2

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

getString()基于零的所以在你的情况下是返回"first"。然后你通过from作为类的from参数(我想,扩展了SimpleCursorAdapter),然后当适配器试图映射这个列名称时收到这个错误。在这种情况下,from表示保存要绑定到UI的数据的列名列表,其表示为to

0

在从参数中

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

需要的列名作为参数,而要传递柱从数组值阵列,而“到”是布局textviews这些列的ID所需德预计,所以在这个例子中的id是android.R.id.textview1,所以以下:

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

,然后对这些参数进行查询。