ユーザーがデータを入力するために多くのスピナーを使用しています。データはSQLiteデータベースに保存されます。各 Spinner には、データベース テーブルからのデータが入力されます。
データが表示される順序を制御したい - たとえば、以下は「数量」スピナー コードです。
// QUANTITY SPINNER
Cursor quantityCursor = rmDbHelper.fetchAllQuantities();
startManagingCursor(quantityCursor);
// create an array to specify which fields we want to display
String[] from6 = new String[]{RMDbAdapter.QUANTITY_FORM_TEXT};
int[] to6 = new int[]{android.R.id.text1};
// create simple cursor adapter
SimpleCursorAdapter quantitySpinnerAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, quantityCursor, from6, to6 );
quantitySpinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
// get reference to our spinner
quantitySpinner.setAdapter(quantitySpinnerAdapter);
if (damagedComponentId > 0) { // Set spinner to saved data
int spinnerPosition = 0;
for (int i = 0; i < quantitySpinner.getCount(); i++)
{
Cursor cur = (Cursor)(quantitySpinner.getItemAtPosition(i));
//--When your bind you data to the spinner to begin with, whatever columns you
//--used you will need to reference it in the cursors getString() method...
//--Since "getString()" returns the value of the requested column as a String--
//--(In my case) the 4th column of my spinner contained all of my text values
//--hence why I set the index of "getString()" method to "getString(3)"
int quantitySpinnerItem = cur.getInt(1);
if(quantitySpinnerItem == quantitySpinnerData)
{
//--get the spinner position--
spinnerPosition = i;
break;
}
}
quantitySpinner.setSelection(spinnerPosition);
}
else { // Set spinner to default
int spinnerPosition = 0;
for (int i = 0; i < quantitySpinner.getCount(); i++)
{
Cursor cur = (Cursor)(quantitySpinner.getItemAtPosition(i));
int quantitySpinnerItem = cur.getInt(1);
if(quantitySpinnerItem == 1)
{
//--get the spinner position--
spinnerPosition = i;
break;
}
}
positionSpinner.setSelection(spinnerPosition);
}
1、10、11、12、13、14、15、16、17、18、19、2、20、21などのようにデータをソートすることを除いて、これは正常に機能します。明らかに、1、 2、3など
これで、(スピナーの代わりに) 「NumberPicker」を使用するか、01、02、03 を使用してデフォルトの並べ替えを回避できることがわかりましたが、スピナーがデータを表示する順序を変更する方法はありますか?
動作する可能性のある 2 つのオプションを確認しました (ただし、コードに追加する場所がわかりません)。
1) Collections.sort(SourceArray) を次のように使用します: Android での Spinner データの並べ替え。ただし、Spinner でこれを機能させることはできません。
または多分これ:
2)ここでアドバイスされているように、データベースを照会するときに「IDによる順序」を設定します:SimpleCursorAdapterでリストビューアイテムを再配置します。しかし、どこでこれを行うのかわかりません..
どんなアドバイスも大歓迎です..
編集 - 以下は、要求されたデータベース ヘルパー クラスのコードです。
public Cursor fetchAllQuantities() {
return rmDb.query(QUANTITY_TABLE, new String[] {
QUANTITY_ID,
QUANTITY_FORM_TEXT},
null, null, null, null, QUANTITY_ID);
}
ORDER BY ステートメント (QUANTITY_ID) を含めましたが、Spinner がデータを表示する順序には影響しません。
EDIT 2 - 最後のステートメントをスクラッチします。正しく順序付けされていますが、データベースが最初に作成されると、デフォルトの順序で数字が入力されます。以下は、データを入力する場所です。
db.execSQL("INSERT INTO " + QUANTITY_TABLE + "(" + QUANTITY_FORM_TEXT + ")" +
" SELECT '1' AS " + QUANTITY_FORM_TEXT +
" UNION SELECT '2' " +
" UNION SELECT '3' " +
" UNION SELECT '4' " +
" UNION SELECT '5' " +
" UNION SELECT '6' " +
" UNION SELECT '7' " +
" UNION SELECT '8' " +
" UNION SELECT '9' " +
" UNION SELECT '10' " +
" UNION SELECT '11' " +
" UNION SELECT '12' " +
" UNION SELECT '13' " + etc.
しかし、SQLite プログラムでデータベース テーブルを見ると、次のようになります。
_id | quantity_form_text
1 | 1
2 | 10
3 | 11
4 | 12
5 | 13
6 | 14 etc.
ですから、私の質問は、そもそもデータベースに入れられるデータをどのように制御するのですか??