2011-04-30 44 views
1

我有下面的代碼應該顯示在列表中的內容從一個數據庫(某個表和某個列),但我得到一個強制關閉對話框,我不知道爲什麼。強制關閉創建simplecursor適配器

這是我的代碼:

public class Server8 extends Activity { 

DBAdapter db; 

    @Override 
    public void onCreate (Bundle savedInstanceState) { 

     setContentView(R.layout.main); 

     db=new DBAdapter(this); 
     db.openDataBase(); 
     Cursor cursor=db.getAllData2(); 

     startManagingCursor(cursor); 

     String[] from=new String[] {db.KEY_ROUTE};  

     int[] to = new int[] { R.id.name_entry}; 

     ContactListCursorAdapter items = new ContactListCursorAdapter(this, R.layout.list_example_entry, cursor, from, to); 
    } 

    public class ContactListCursorAdapter extends SimpleCursorAdapter{ 

     private Context context; 

     private int layout; 

     public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { 
      super(context, layout, c, from, to); 
      this.context = context; 
      this.layout = layout; 
     } 

     @Override 
     public View newView(Context context, Cursor cursor, ViewGroup parent) { 

      Cursor c = getCursor(); 

      final LayoutInflater inflater = LayoutInflater.from(context); 

      View v = inflater.inflate(layout, parent, false); 

      int nameCol = c.getColumnIndex(db.KEY_ROUTE); 
      String name = c.getString(nameCol); 

      TextView name_text = (TextView) v.findViewById(R.id.name_entry); 
      if (name_text != null) { 
       name_text.setText(name); 
      } 

      return v; 
     } 

     @Override 
     public void bindView(View v, Context context, Cursor c) { 

      int nameCol = c.getColumnIndex(db.KEY_ROUTE); 
      String name = c.getString(nameCol); 

      TextView name_text = (TextView) v.findViewById(R.id.name_entry); 

      if (name_text != null) { 
       name_text.setText(name); 
      } 
     } 

在我將對DBAdapter我有這樣的:

public static final String TABLE_2= "route"; 

public static final String KEY_ROWID_2="_id"; 

public static final String KEY_ROUTE= "route"; 

public static final String KEY_USER_ID= "user_id"; 

這是我如何查詢光標:

public Cursor getAllData2() 
{ 
    return db.query(TABLE_2, new String[] {KEY_ROUTE},null,null,null,null,null); 
} 

,這是我的logcat給我:

9月4日至三十○日:26:24.323:ERROR/AndroidRuntime(2676):java.lang.IllegalArgumentException異常:致列 '_id' 不存在

九月4日至30日:26:24.323:ERROR/AndroidRuntime(2676):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)

九月4日至30日:26:24.323:ERROR/AndroidRuntime(2676 ):at android.widget.CursorAdapter.init(CursorAdapter.java:111)

04-30 09:26:24.323:ERROR/AndroidRuntime(2676):at android.widget.CursorAdapter。( CursorAdapter.java:90)

04-30 09:26:24.323:ERROR/AndroidRuntime(2676):在android.widget.ResourceCursorAdapter(ResourceCursorAdapter.java:47)

04-30 9點26分:24.323:ERROR/AndroidRuntime(2676):at android.widget.SimpleCursorAdapter。(SimpleCursorAdapter.java:88)

04-30 09:26:24.323:ERROR/AndroidRuntime(2676):at com.server.Server8 $ ContactListCursorAdapter。(Server8.java:103)

04-30 09:26:24.323:ERROR/AndroidRuntime(2676):at com.server.Server8.onCreate(Server8.java:91)


產生的原因:java.lang.IllegalArgumentException異常:列「_id」不存在

我還沒有問這樣的事情!

,這就是我的TABLE_2樣子:

_id   route     user_id 

1   Sebes-Alba    1    ....only one record 

我做錯了嗎?由於

回答

2

確保您的表格有一個名爲_id的列。常用的方法是創建_id列爲_id INTEGER PRIMARY KEY AUTOINCREMENT
然後,確保您始終使用對數據庫的每個查詢查詢_id

參考文獻:

Details on column ID issues

How to create a column

+0

它有_id。 .........所以這個查詢不好:return db.query(TABLE_2,new String [] {KEY_ROUTE},null,null,null,null,null); ? – adrian 2011-04-30 22:00:24

+0

嘗試用'KEY_ROWID_2'就像這樣'return db.query(TABLE_2,new String [] {KEY_ROWID_2,KEY_ROUTE},null,null,null,null);' – Haphazard 2011-04-30 22:06:02

+0

我確實.....我有沒有錯誤...謝謝;) – adrian 2011-04-30 22:10:38

0

你的日誌說有你的查詢(表)的目標沒有_id柱:

列「_id」不存在

您應該驗證,如果你有一個在你的route表中列名爲_id,如果有差異,你應該更新你的public static final String KEY_ROWID_2

+0

我明白我在問這樣一個列,它不存在...但我不要求它....是的,它存在...我該怎麼處理它.........我不明白你的意思是通過更新我的公共靜態最終STring KEY _...... – adrian 2011-04-30 21:57:34