2011-12-16 7 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なし(私は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

だから、あなたはlistview naでアイテムIDを表示したいですか? – Praveenkumar

答えて

0

- - このようなあなたのメインの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; 
} 

別のレイアウトの下に... geez

0

あなたの「ID-フィールドということは非常に重要です"は" _id "という名前を持っています。そうでないと動作しません。

+0

ちょっと大変だから、サンプルに従っていたけど、ええ、 –

0

あなたのリストビューの行のXMLはどのように見えますか? IDを正しく設定しましたか?

EDIT:
あなたのXMLファイルが正しく設定されているように見えるので、私は今、あなたの問題は何か他のものであると信じています。 bug_idフィールドのデータがデータベースに正しく格納されていないか、データベースから正しく取得されていない可能性があります。

これはアンドロイドのドキュメントです:
'レコードのIDに「_id」という名前の定数の列(定数_ID付き)が含まれていることを確認してください。すべてのレコード間でユニークな別のフィールド(URLなど)を持っているかどうかに関わらず、このフィールドを持つ必要があります。このようなあなたのbug_idフィールドに別名を与えるためにあなたの作成、テーブルのSQLを変更する

INTEGER PRIMARY KEY AUTOINCREMENT」

試してみてください:

あなたはSQLiteデータベースを使用している場合は、_IDフィールドには、次のタイプでなければなりません
bug_id as _id integer primary key autoincrement, 

とは対照的に、このコード&から

bug_id integer primary key autoincrement, 
+0

にxmlコードが追加されています。 –

+0

編集済みの回答。 – Ozzy

+0

bug_id整数型の主キーautoincrement + sqliteは本当に好きだと思われますが、それは "as"によって引き起こされたと言います –

0

単に取得のIDのごDatabase.javaファイルにこのコードを貼り付け

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

レイアウト幅だけ5pxの、私はそれはおそらくdissapeared 3pxしてテキストを埋めたときだった:問題が見つかり

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}); 
関連する問題