【发布时间】:2010-06-18 22:25:21
【问题描述】:
我正在使用此示例代码来填充 Spinner。从数据库中读取数据。选择正确显示 - 在这种情况下,它显示“绿色”和“红色”。
Spinner spinnerColor = (Spinner) findViewById(R.id.spinnertProfile);
mProfileDbHelper = new ProfileDbAdapter(this);
mProfileDbHelper.open();
Cursor profilesCursor = mProfileDbHelper.fetchAllProfiles();
startManagingCursor(profilesCursor);
// Create an array to specify the fields we want to display in the list
String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
// and an array of the fields we want to bind those fields to
int[] to = new int[] { R.id.textviewColors };
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
R.layout.profile_color, profilesCursor, from,
to);
spinnerColor.setAdapter(profilesAdapter);
}
但是,当我更改为使用不同的布局时 android.R.layout.simple_spinner_dropdown_item。微调器文本消失了。
// Now create a simple cursor adapter and set it to display
SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
R.layout.profile_color, profilesCursor, from,
to);
profilesAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(profilesAdapter);
查看以下没有和使用 simple_spinner_dropdown_item 的快照:
我可能会错过什么吗?
【问题讨论】:
-
textviewColors用在simple_spinner_dropdown_item中吗?我会仔细检查是否有错别字。 -
Qberticus,我不确定我明白你在说什么。我是一个 Android 新手 :-) textviewColors 是 layout/profile_color.xml 中 TextView 的 ID,其中 profilesAdapter 在实例化为 SimpleCursorAdapter 时指代。
<?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="fill_parent"> <TextView android:text="" android:id="@+id/textviewColors" android:layout_width="wrap_content" android:layout_height="wrap_content"> </TextView> </LinearLayout>
标签: android android-widget spinner