简体   繁体   English

不使用SimpleCursorAdapter从数据库填充ListView

[英]Populating ListView from Database without Using SimpleCursorAdapter

I have a seperate DatabaseHandler("db") Class which extends SQLiteOpenHelper BUT NOT ACTIVITY which means this is a seperate class file which is not related to any activity. 我有一个单独的DatabaseHandler(“ db”)类,该类扩展了SQLiteOpenHelper但没有活动,这意味着这是一个与任何活动都不相关的单独的类文件。 If you ask why i have such a class not in activity because i like keeping my files organized. 如果您问为什么我没有活动此类课程,因为我喜欢保持文件井井有条。

So i have listview in a fragment and i have a "DatabaseHandler.getAllRows()" function in that class which returns a "List" Object. 所以我在一个片段中有listview,并且在该类中有一个“ DatabaseHandler.getAllRows()”函数,该函数返回“ List”对象。 Then i want to use this list to populate my listview. 然后,我想使用此列表来填充我的列表视图。

So when this class is a seperate class which is not related to any activity, i can't use simplecursorAdapter since it wants a context in paramteres part when creating with new(i tried to send parameter as context but didnt work) so i need to use something else.... 因此,当此类是与任何活动无关的单独的类别时,我不能使用simplecursorAdapter,因为它在使用new创建时希望在参数部分中有一个上下文(我尝试将参数作为上下文发送,但没有用),所以我需要使用其他东西...

I can change return type, i can use another thing instead of listview.. just give me a good advice for how to populate listview or show table rows NICELY. 我可以更改返回类型,我可以使用其他东西代替listview ..只是给我一个很好的建议,如何填充listview或很好地显示表行。

If you want a real simple solution, you can store your rows as an array, and use ArrayAdapter to display them. 如果您想要一个真正简单的解决方案,则可以将行存储为数组,并使用ArrayAdapter来显示它们。 You can override the getView to display the results in your preferred way. 您可以重写getView以首选方式显示结果。

If this solution is not good enough, I think you will have to implement your own Adapter , which might be a good idea anyway. 如果此解决方案不够好,我认为您将必须实现自己的Adapter ,无论如何这可能是个好主意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM