2011-12-16 52 views
0

我在這裏有點困惑。如何從表中獲取_id字段並將其放入ListView中

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor, 
      new String[] {db.KEY_ROWID, db.BUGS_DATE, db.BUGS_DESCRIPTION}, 
      new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description}); 

這基本上顯示了一個帶日期和描述但沒有id的ListView(我只是用空格代替了id)。 _id字段是主鍵,它是一個整數。

看看它是如何看的IMG

enter image description here

XML文件:

<?xml version="1.0" encoding="utf-8"?> 
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/tableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1" 
    > 
     <TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"> 
      <TextView    
      android:id="@+id/bug_id" 
      android:layout_width="5dip" 
      android:layout_height="wrap_content" 
      android:padding="3dip"      
      > 
      </TextView> 
      <TextView    
      android:id="@+id/bug_date" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:padding="3dip" 
      android:gravity="right" 
      > 
      </TextView> 
      <TextView    
      android:id="@+id/bug_description" 
      android:layout_width="180dip" 
      android:layout_height="wrap_content" 
      android:padding="3dip" 
      android:gravity="right" 
      > 
      </TextView> 
     </TableRow> 
    </TableLayout> 

創建表查詢:

BUGS_CREATE = "CREATE TABLE bugs (_id INTEGER NOT NULL PRIMARY KEY, date DATE DEFAULT (DATETIME('NOW')) NOT NULL, description TEXT) "; 
+0

所以,你想顯示項目ID與列表視圖NA? – Praveenkumar 2011-12-19 13:34:15

回答

0

發現了問題:

android:layout_width="5dip"    
    android:padding="3dip" 

佈局寬度只有5像素,當我通過3px的填充文字,它可能dissapeared低於另一個佈局... geez

0

這是你的「ID場非常重要「確實有名字」_id「,否則它可能不起作用。

+0

kinda dunnno爲什麼它很重要,我正在關注樣本,但是,它確實有 – 2011-12-16 22:43:09

0

你的列表視圖行的XML是什麼樣的?你是否正確設置了ID?

編輯:
由於XML文件似乎是正確設置,我現在相信你的問題是別的東西。可能是因爲沒有正確地從數據庫中存儲或檢索到bug_id字段的數據。

這是來自android的文檔:
'確保爲記錄的ID包含一個名爲「_id」(帶有常量_ID)的整數列。無論您是否有其他字段(如URL)在所有記錄中都是唯一的,您應該擁有此字段。如果您使用的SQLite數據庫中,_ID字段應該是以下類型:

INTEGER PRIMARY KEY AUTOINCREMENT」

試圖改變自己創建表的SQL給你bug_id領域的別名是這樣的:

bug_id as _id integer primary key autoincrement, 

,而不是:此代碼&

bug_id integer primary key autoincrement, 
+0

添加了xml代碼。 – 2011-12-17 13:12:20

0

只要得到ID的這段代碼粘貼到您的Database.java文件 -

String[] return_result() 
{ 

    SQLiteDatabase db = this.getReadableDatabase(); 
    Cursor cursor = db.query(tableName, new String[] { "_id", "date", "description"},null, null, null, null, null); 
    String[] result = new String[cursor.getCount()]; 
    int i = 0; 
    if(cursor.moveToFirst()) 
    { 
     do 
     { 
      result[i]= cursor.getString(cursor.getColumnIndex("_id")); 
      i++; 
     }while(cursor.moveToNext()); 
    } 
    if (cursor!=null && !cursor.isClosed()) 
    { 
     cursor.close(); 
     db.close(); 
    } 
    return result; 
} 

在你主要的Java文件,使用這樣的 -

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.bugs_list_item, itemCursor, 
     new String[] {db.return_result(), db.BUGS_DATE, db.BUGS_DESCRIPTION}, 
     new int[] {R.id.bug_id, R.id.bug_date, R.id.bug_description}); 
相關問題