Android:How to customize CursorAdapter(如何自定义CursorAdapter)
2013-07-23 18:07
627 查看
原文地址:http://thinkandroid.wordpress.com/2010/01/11/custom-cursoradapters/
Hey everyone,
Now that we have the SimpleCursorAdapter figured out, when writing your Custom Cursor Adapter you’ll see where some of the parameters come into play. Here’s an example of a Custom Cursor Adapter that I built:
The Custom Cursor Adapter created above extends the SimpleCursorAdapter and also implements the Filterable class (which I’ll get to later). Here you can see the importance of passing in the layout of the list row entry as in the cursor adapter when the actual
views in the list are being built (via newView and bindView) and both methods use this layout to inflate the view, which you can then use to retrieve the TextView / ImageView / etc that are custom designed in your list entry row XML file.
In my example, when a view is created for the first time (via newView), you first inflate the view and retrieve the cursor WHICH YOU PASSED IN. Remember this as depending on which columns you told your cursor to return, those are the columns that you can retrieve
information from in your Custom Cursor Adapter. In other words, if my cursor looked like:
And I tried to retrieve the People.NUMBER column from my cursor, it will return an SQL exception.
The rest should be pretty self explanatory – simply grab the data you want and do what you want with the data (i.e. calculations, grabbing images, etc) and then put them into your inflated views.
One more thing to note is that YOU MUST place something into each view (even if it is NULL). For instance, had my adapter looked like:
Then you’ll notice some weird behavior – namely, that you’ll see the names start shifting as you scroll up and down the list. What happens is that if you don’t instantiate and place something into your TextView (basically to act as a place holder) then in your
bindView method nothing gets bound to some of the TextViews and thus the shifting. So basically, if you see stuff shifting around in your lists, then that’s a big flag for make sure you are binding things to all of your views in both your newView and bindView
methods.
Finally, a little on the Filterable implementation. If you want a list that filters as the user starts to type then this is what you need to do. Once you implement the Filterable class, you’ll need to override the runQueryOnBackgroundThread() method and this
code snippet:
And basically the StringBuilder is just building the constraint that the cursor will run. If you want to mess with this, then you’ll need to keep:
But you can customize the rest of the constraint, for instance:
And that’s it! Next post will be on BaseAdapters, and I’ll also talk a little on the comparison between these two adapters.
To see all posts on CursorAdapters, please visithttp://thinkandroid.wordpress.com/category/android-tutorials/cursoradapter-tutorials/
Happy coding.
- jwei
Hey everyone,
Now that we have the SimpleCursorAdapter figured out, when writing your Custom Cursor Adapter you’ll see where some of the parameters come into play. Here’s an example of a Custom Cursor Adapter that I built:
views in the list are being built (via newView and bindView) and both methods use this layout to inflate the view, which you can then use to retrieve the TextView / ImageView / etc that are custom designed in your list entry row XML file.
In my example, when a view is created for the first time (via newView), you first inflate the view and retrieve the cursor WHICH YOU PASSED IN. Remember this as depending on which columns you told your cursor to return, those are the columns that you can retrieve
information from in your Custom Cursor Adapter. In other words, if my cursor looked like:
The rest should be pretty self explanatory – simply grab the data you want and do what you want with the data (i.e. calculations, grabbing images, etc) and then put them into your inflated views.
One more thing to note is that YOU MUST place something into each view (even if it is NULL). For instance, had my adapter looked like:
bindView method nothing gets bound to some of the TextViews and thus the shifting. So basically, if you see stuff shifting around in your lists, then that’s a big flag for make sure you are binding things to all of your views in both your newView and bindView
methods.
Finally, a little on the Filterable implementation. If you want a list that filters as the user starts to type then this is what you need to do. Once you implement the Filterable class, you’ll need to override the runQueryOnBackgroundThread() method and this
code snippet:
To see all posts on CursorAdapters, please visithttp://thinkandroid.wordpress.com/category/android-tutorials/cursoradapter-tutorials/
Happy coding.
- jwei
相关文章推荐
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- Android 如何在自定义界面上启用输入法 (How to enable inputmethod for the custom UI)
- (OK) how to customize the kernel for your hardware for Android-x86
- How-to: Customize Android progress bars
- Android:How to use loader in Android(如何使用Loader)
- 思考Android架构(二):像Android框架,如何(How-to)吸引开发者来使用它呢?
- How to Secure an Android App 如何保护Android应用程序
- 思考ANDROID架构(4):HOW-TO, 如何从API洞悉软件的话语权
- How to stream video to your android phone 如何将视频远程传输至你的android手机?
- [How to]如何自定义plist文件和读取plist文件内容
- android中使用CursorAdapter和SimpleCursorAdapter的方法
- 思考ANDROID架构(二):像ANDROID框架,如何(HOW-TO)吸引开发者来使用它呢?
- 自定义(手动调整).net中图像的调色板(How to adjust (customize) image's palette in .net?)
- 思考ANDROID架构(4):HOW-TO, 如何从API洞悉软件的话语权
- How To Create Custom Skins For DotText(如何为DotText创建自定义风格的皮肤)
- How to get camped network type name - Android应用如何获得Phone当前注册的网络类型
- 如何为Android Spinner设置一个初始值(How to make an Android Spinner with initial text “Select One”)?
- How to disable cursor positioning and text selection in an EditText? (Android)