1

データベースのレコードをリストビューに表示する際に問題があります。これは私のコードです

public class FirstTab extends ListActivity {    


private DatabaseHelper dbhelper;


@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);

    setContentView(R.layout.first);

    dbhelper = new DatabaseHelper(getApplicationContext());
    Cursor cursor = dbhelper.getAllNotes();
    startManagingCursor(cursor);


    String[] columns = new String[] {DatabaseHelper.colTitle , DatabaseHelper.colDate};

    int[] to = new int[] { android.R.id.text1, android.R.id.text2};

    SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);

    setListAdapter(mAdapter);
}
}

...
public Cursor getAllNotes()
 {
     SQLiteDatabase db=this.getReadableDatabase();

     return db.query(noteTable, new String [] {colTitle, colDesc, colDate}, null, null, null, null, null);

 }
...

さらに必要な場合は、ここにレポがあります https://github.com/grzegorz-l/myHomeworks

アプリを起動すると、開始時にクラッシュします (RuntimeException)。onCreate メソッドの最後の 2 行にコメントすると実行されますが、ofc には何も表示されません。

前もって感謝します

グレッグ

4

2 に答える 2

2

getApplicationContext()SimpleCursorAdapter の作成時に渡さないでください。this代わりに、ListActivity のコンテキストを使用してください。

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
于 2011-04-08T21:04:04.743 に答える
0

FirstTab.java ファイルは ListActivity を拡張しています。これは、レイアウト ファイルに android:id が次のように設定された ListView が含まれている必要があることを意味します。

android:id="@android:id/list"

また、FirstTab.java には note_entry.xml を指すレイアウトがあります。上記の説明の ListView を含むレイアウトを指すか、ListActivity によって拡張されないようにする必要があります。

于 2011-04-09T02:10:31.997 に答える