您的位置:首页 > 移动开发 > Android开发

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐