Android - Does SimpleCursorAdapter allow multiple layouts like BaseAdapter?

帅比萌擦擦* 提交于 2019-12-06 05:16:42

问题


I know you can create a custom Adapter extending BaseAdapter and create various layouts which can be inflated depending on which row the AdapterView is at..

But is there any way to get a simple amount of customization with a SimpleCursorAdapter?

Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.

Will SimpleCursorAdapter do? Or are there any elegant solutions for this?

Cheers


回答1:


But is there any way to get a simple amount of customization with a SimpleCursorAdapter?

Just like BaseAdapter, you can extend CursorAdapter or SimpleCursorAdapter to do your customization.

Eg. I have a database and I would like to query it and return the results to a ListView with alternating row layouts.

If you are only alternating a minor thing like row color, you can simply override bindView() and check if(cursor.getPosition() % 2 == 0) (or == 1) to set the appropriate background color.

If you are using different types of or numbers of Views in each layout, you need to override getViewTypeCount() and getItemViewType(). Then use getItemViewType() in newView() to load the appropriate layout and in bindView() to display the appropriate data.



来源:https://stackoverflow.com/questions/13942238/android-does-simplecursoradapter-allow-multiple-layouts-like-baseadapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!