私はこれを理解できないようですSimpleCursorAdapter
.1つのエラーを修正するたびに、別のエラーがポップアップし、手順に従ってそのエラーを修正すると、最初のエラーが再び表示されます. 私はここでぐるぐる回っているように感じるので、ここにデバッグしようとしているコードのチャンクがあります.最初の部分はDBを作成するだけですが、それを理解するのに何か役立つと思いました.
SQLiteDatabase rpgDB = null;
String classFields = " (classID, className, classHP)";
try {
rpgDB = this.openOrCreateDatabase("RpgDB", MODE_PRIVATE, null);
rpgDB.execSQL("DROP TABLE IF EXISTS " + classTable);
rpgDB.execSQL("CREATE TABLE IF NOT EXISTS " + classTable + " (classID INT(3), className TEXT, classHP INT(4));");
rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (1, 'Warrior', 10);");
rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (2, 'Rogue', 7);");
rpgDB.execSQL("INSERT INTO " + classTable + classFields + " VALUES (3, 'Mage', 5);");
String query = "SELECT className AS _id FROM " + classTable;
Cursor cursor = rpgDB.rawQuery(query, null);
rpgDB.close();
int[] to = new int[] { R.id.classDropDown };
String[] spinnerFields = new String[] { "_id" };
/**
* Test code to check value of cursor
*/
int count = cursor.getCount();
cursor.moveToFirst();
while (cursor.isAfterLast() == false)
{
String test = cursor.getString(0);
cursor.moveToNext();
}
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.main, cursor, spinnerFields, to);
int cursorcount = cursorAdapter.getCount();
// cursorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner classDropDown = (Spinner) this.findViewById(R.id.classDropDown);
classDropDown.setAdapter(cursorAdapter);
} catch (Exception ex) {
System.out.println("Exception: " + ex);
}
SimpleCursorAdapter から Spinner を設定するのは、途方もなく面倒なようです。
編集:このコードのエラーは
Invalid statement in fill window
rpgDB.close() 行をコメントアウトすると
Spinner is not a view that can be bounds by this SimpleCursorAdapter
selectステートメントとspinnerfieldsを(DBをコメントアウトして)変更すると:
String query = "SELECT className FROM " + classTable;
String[] spinnerFields = new String[] { "className" };
私は得る:
column '_id' does not exist
EDIT XML:ここに main.xml (id main) XML があります
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="@color/white" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:textSize="11pt"
android:text="@string/hello"
android:textColor="@color/baseTextColor" />
<Spinner
android:id="@+id/classDropDown"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
説明:したがって、将来誰かがこれに遭遇した場合、Luksprog のおかげで、私はこれを少しよく理解できます (そして、彼は私に修正を示しました):
android.R.id および android.R.layout の「ids/layouts」は、TextField や Spinner の「リスト」レイアウトなどの Android コンポーネントのデフォルトの xml レイアウトです。Spinner オブジェクトは、基本的にそれ自体では完全ではありません。リスト自体の外観を定義するための別の XML 部分と、行用の別の XML 部分が必要です (text1 はデフォルトの Android TextField です)。
つまり、「to」フィールドを TextField に割り当てるか、android.id.text1 などのデフォルトの Android フィールドのいずれかを使用しますが、それを使用するには、デフォルトの Android フィールドを含むレイアウトも使用する必要があると思います、andoid.R.layout (私の場合、simple_spinner_item)。最後に、dropdownresource を独自の xml または Android のデフォルトに設定します (私の場合は android.R.layout.simple_spinner_dropdown_item でした)。