[email protected]
我正在从spinner.getSelectedItem()。toString()调用返回文本'[email protected]'。我不知道为什么。微调器绑定到SimpleCursorAdapter。[email protected]
下面是代码
cCategories = (Cursor) myAdapter.getAllCategories();
this.startManagingCursor(cCategories);
SimpleCursorAdapter scaCategories = new SimpleCursorAdapter(this, R.layout.track_category_item,cCategories,new String[] {DBAdapter.KEY_CATEGORIES_NAME},new int[]{R.id.text1});
scaCategories.setDropDownViewResource(R.layout.track_category_dropdown_item);
mCatSpinner = (Spinner) findViewById(R.id.thecategory);
mCatSpinner.setAdapter(scaCategories);
if(mCatSpinner.isSelected() != true) {
mCatSpinner.setSelection(0);
}
和XML track_category_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:id/text1"
style="?android:attr/spinnerItemStyle"
android:ellipsize="marquee"
android:singleLine="true">
</TextView>
track_category_dropdown_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee" />
的微调XML看起来像这样
<Spinner
android:id="@+id/thecategory"
android:prompt="@string/SELECT_CATEGORY"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="15px"
android:layout_y="133px" >
</Spinner>
并恢复光标
public Cursor getAllCategories()
{
return db.query(DATABASE_CATEGORIES_TABLE, new String[] {
KEY_CATEGORIES_ROWID,
KEY_CATEGORIES_NAME,
KEY_CATEGORIES_DEFAULT},
null,
null,
null,
null,
null);
}
的微调似乎正常工作。当我尝试保存时,这是使用spinner.getSelectedItem()。toString()作为选定项目的值传递的内容。
任何人都可以在这里看到任何错误的东西。不知道该怎么办。
感谢 帕特里克
你的代码工作,你写的。 Spinner
是AdapterView
。您连接到的适配器是SimpleCursorAdapter
。这意味着所选项目是Cursor
(位于游标结果集中与用户选择对应的项目中)。 Cursor
的默认实现为toString()
,它返回类似[email protected]
的内容。
由于您没有告诉我们您要做什么,因此不可能为您进一步准确地提供建议。但是,无论您想要保存的是什么,都需要从中取出,您从getSelectedItem()
中获得。
谢谢commonsware,我想你可能已经回答了我的问题。我只是试图获取所选项目的值,并将其保存到数据库表中。最初我使用ArrayAdapter来填充微调器。该数组是一个具有几个类别(“业务”,“个人”)的单元素数组。这工作作为一个字符串已通过。 所以你说我需要做一些额外的代码来根据传入的spinner.getSelectedItemPosition()来从游标中找到实际的类别名? 谢谢 patrick – bugzy 2010-01-16 02:19:07
是的。鉴于你的query(),你需要在'Cursor'上调用'getString(1)'来取回第二列......假设'KEY_CATEGORIES_NAME'是你所寻求的值。 – CommonsWare 2010-01-16 12:38:06
我可能会困扰读你的背景,但只是想简单的帮助。 我有一个以DbHelper.KEY_COL
命名的列,我正在检索特定行的DbHelper.KEY_COL
值。 也许我的一些代码会有所帮助:
Cursor colCur=(Cursor)spCols.getSelectedItem();
String col=colCur.getString(colCur.getColumnIndex(DbHelper.KEY_COL));
这对于ArrayAdapter来说是正确的。然后传递的值是类别的文本(Business或Personal)。我使用SimpleCursorAdapter的事实是否改变了值的传递方式? – bugzy 2010-01-16 01:40:18