Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 11 years ago.
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
这是我的 5 美分。我有类似的问题。我正在使用 SimpleCursorAdapter,它实现了 SpinnerAdapter 接口,但直到 SDK 版本 11 (Android 3.0) 才出现。我希望我的应用程序能够与 SDK 8 (Android 2.2) 及更高版本一起使用,因此我必须将 SimpleCursorAdapter 替换为另一个或我自己的。真正的挑战是我还为微调器使用了自定义 XML 布局,并在其中显示了光标(即光标适配器)中的多个字段。这是我经过大量研究后的解决方案,而这些信息并不容易获得。
这是 Spinner 中使用的名为 spin_layout.xml 的布局文件:
这是实现 SpinnerAdapter 并扩展(用作小助手)BaseAdapter 的适配器。最初使用的 Cursor 被转换为 List 并与包含微调器的 Activity 一起传入构造函数。
与您在网络上找到的其他解决方案不同,方法 getItemId 与数据库中的 id 字段建立链接,就像 SimpleCursorAdapter 一样。该 id 是在 spinner.setOnItemSelectedListener 的 OnItemSelectedListener 中的 onItemSelected(AdapterView arg0, View arg1, int position, long id) 中传递的参数。方法getView扩展了spin_layout.xml,识别布局中包含的两个视图并为它们分配值(作为字符串!)。
Here is my 5 cents. I had a similar problem. I was working with SimpleCursorAdapter which implements interface SpinnerAdapter, but arrived only until SDK version 11 (Android 3.0). I intended my app to work with SDK 8 (Android 2.2) and up, so I had to replace SimpleCursorAdapter with another, or my own. The real challenger was that I also used a custom XML layout for spinner and in it showed several fields from cursor i.e. cursor adapter. So here is my solution after a lot of research, and the info wasn't easy to come by.
Here is the layout file used in spinner named spin_layout.xml :
And here is the adapter implementing SpinnerAdapter and extending (using as a little helper) BaseAdapter. The Cursor that was used originally was transformed into List and passed in constructor, together with activity containing the spinner.
Unlike other solutions you find on the web, method getItemId establishes link with id field from database, just like SimpleCursorAdapter. That id is the argument passed in onItemSelected(AdapterView arg0, View arg1, int position, long id) in OnItemSelectedListener for spinner.setOnItemSelectedListener. Method getView inflated spin_layout.xml, identifies the two views contained in layout and assigns them values (as String!).
这是一个简单示例。不要被“光标”名称所迷惑,它只是使用一个列表。这个想法很简单:从
BaseAdapter
扩展并实现任何缺少的方法(它是一个抽象类);并且不要忘记重写getView()
方法以提供Category
的“视觉”表示。This is a simple example. Don't be fooled by the "cursor" name, it's just using a List. The idea is simple: extend from
BaseAdapter
and implement any missing methods (it's an abstract class); and don't forget to override thegetView()
method to provide the "visual" representation of yourCategory
.我不确定这是否有帮助,但 Android SDK 有一个很好的小示例,说明从数组向 Spinner 提供数据。将列表转换为数组很简单,所以也许这会为您指明正确的方向。
http://developer.android.com /resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html
祝你好运。
I am not sure if this helps, but the Android SDK has a nice little example of supplying data to a Spinner from an array. Converting a List to an array is trivial, so maybe this will point you in the right direction.
http://developer.android.com/resources/samples/Spinner/src/com/android/example/spinner/SpinnerActivity.html
Good luck.