在创建列表视图活动之前预创建行视图?
我有一个活动,其中有两个选项卡和一个列表视图。我想让活动的加载和打开速度非常快。我担心填充两个列表选项卡会导致应用程序在该活动打开时滞后。我正在考虑在应用程序的启动屏幕期间从数据库加载数据并创建一系列视图,然后在活动启动后将它们简单地扔到列表视图上。这值得付出努力吗?我似乎找不到一种简单的方法来做这样的事情。我假设我需要自定义阵列适配器。有没有人有做这样的事情的经验?或者您只是建议坚持在 onCreate() 中使用游标适配器的标准?
I have an activity that has two tabs and a list view in each. I want to make loading and opening of the activities very fast. I'm worried that populating both list tabs will cause the app to lag when that acivity opens. I'm thinking of loading data from my database during the splash screen of my app and creating an array of views, then simply throwing them onto the listviews once that acivity is launched. Is this worth the effort? I can't seem to find a simple way to do something like this. I'm assuming I will need custom array adapters. Does anyone have any experience doing somethng like this? Or would you just recommend sticking to the standard of using a cursor adapter in onCreate() ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是你需要这么快加载它们的原因是什么。一般来说,可以非常快地从数据库中读取项目。尽管如此,你应该在 AsyncTask 中执行此操作,这样你就不会通过此操作阻塞 UI 线程。
另一点是,如果您想在 ListView 中显示数据库中的项目,则不应立即读取所有项目并将它们放入绑定到列表的数组中。相反,您应该使用从数据库查询中获得的 Cursor 并将其绑定到 SimpleCursorAdapter< /a> 或 CursorAdapter 然后将其绑定到 ListView。 CursorAdapter 经过优化,可根据需要从 Cursor 读取数据。请记住,在 Android 上创建对象是一项昂贵的操作,如果不必要,应该避免。因此,如果您的列表非常大并且用户不会总是滚动浏览整个列表,那么为什么要预加载所有项目并为其创建视图呢?
当然,您可以在启动屏幕期间预查询数据库并将返回的游标存储在某处,直到您需要它们为止。
但正如我一开始所说的,我不知道你的应用程序是做什么的,所以很难说它是否值得付出努力。
The question what's the reason for that you need to load them that fast. In general items can be read from a database really fast. Never the less you should do this in an AsyncTask so you won't block the UI thread with this operation.
Another point is, if you want to display items from a database within a ListView you shouldn't read all the item at once and put them into an array that you bind to the list. Instead you should use the Cursor you got from the database query and bind it to a SimpleCursorAdapter or CursorAdapter which you then bind to the ListView. CursorAdapters are optimized to read data from a Cursor as they are needed. Remember that creating objects is an expensive operation on Android which should be avoided if unnecessary. So if your lists a really large and the user properly won't always scroll through the whole list then why preloading all items and creating views for it?
You can of course prequery the database during the splash screen and store the returned cursors somewhere until you need them.
But as I said at the beginning I don't know what your application does so it is hard to say if it is worth the effort or not.