2012-03-13 44 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}; 

,然後對這些參數進行查詢。