2

私はデータベースを使用していて、データベースからカーソルを取得してから、simplecursoradapterを使用してリストビューにデータを入力しています。新しいsimplecursoradapterを作成するとアプリがクラッシュする理由がわからないようです。私のカーソルはシングルトンオブジェクトに含まれています。このクラスのデータ変数を介してそれを参照しています。

String[] columns = new String[] {"item", "quantity", "days"};
int[] to = new int[] { R.id.nametext, R.id.quantitytext, R.id.dayslefttext};
SimpleCursorAdapter sCA = new SimpleCursorAdapter(this, R.layout.itemrow, data.listCursor, columns, to);
listView1.setAdapter(sCA);

simplecursoradapterは、コンストラクターで渡された情報をどのように処理しますか?パラメータの1つが間違った形式ですか?これが機能するかどうかを確認するために使用している各行の単純なxmlファイルは次のとおりです。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content">
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/nametext"/>
  <TextView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/quantitytext"/>
  <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Loading Screen" 
    android:id="@+id/dayslefttext"/>
</LinearLayout>

カーソルをロードする方法は次のとおりです。

listCursor = myDataBase.query(ITEMS_TABLE, null, "location" +" = ?", new String[]{IN_SPECIAL_LIST}, null, null, "item");

問題が何なのかわかりません。これをいくつかのデバッグで実行しましたが、IIが新しいsimplecursoradapterを作成すると問題が発生します。

4

1 に答える 1

5
myDataBase.query(ITEMS_TABLE, null, ...

このnullが問題です。これは、クエリが使用するが、テーブルにSELECT *列がないため、使用する必要があることを意味します。_id

new String[] {"YOURID AS _id", "item", "quantity", "days"}

SimpleCursorAdapterには_id列が必要です

YOURIDは、ROWID(ROWID as _id)、またはDBからの任意のintまたはlongIDである可能性があります

于 2011-04-04T11:10:00.483 に答える