我正在使用一些Spinners来输入数据,然后将数据保存到SQLite数据库中。每个Spinner都填充了数据库表中的数据。
我想控制数据的显示顺序 - 例如,下面是'数量'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'(而不是Spinner)或者我可以使用01,02,03来绕过默认排序,但有没有办法改变Spinner显示数据的顺序?
我看到的两个可能有用的选项(但我看不到添加到我的代码的位置):
1)使用Collections.sort(SourceArray),如下所示:Spinner data sorting in Android。但是,不能让我的Spinner工作。
或者这个:
2)按照此处的建议查询数据库时设置“按ID排序”:Rearrange list view items in 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显示数据的顺序没有影响..
编辑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.
所以我想我现在的问题是你如何控制首先放入数据库的数据?
答案 0 :(得分:0)
好的,所以我似乎对它进行了分类。
我有两个微调器:一个是'Quantity Spinner',它由数据库表填充,只包含数字(如上所示),另一个是'Position Spinner',由表填充数字1-20和文本。
因此,使用数量Spinner,我将Union Select语句(见上文)更改为01,02,03(而不是1,2,3)。由于这是一个只接受整数的列,一旦填充onCreate,数据库表显示1,2,3等,这就是它在Spinner中的显示方式。非常好!
由于位置微调器包含文本,因此该列设置为接受文本。 Union Select语句再次使用01,02,03等编写,但在数据库中保持相同(当它填充Spinner时,它显示01,02,03等 - 这对我的需求来说很好。)
在这两种情况下,David和Mango建议的解决方案都有效,它现在可以通过增量ID来提取数据,并为数字等提供正确的顺序。
希望这是有道理的!谢谢,戴夫。