在初始化 ListView 之前在单独的线程中下载数据
我的应用程序中存在多线程问题。我知道有很多关于 Threads/AsyncTasks/etc 的帖子,但似乎没有一个能解决我的具体问题。
基本上,我在搜索活动中获取一个查询字符串,然后将其发送到我的结果活动,其中该字符串用作 SQL 查询,结果作为 JSON 对象数组返回,然后在 ListView 中显示这些对象(这是结果活动的一部分)。我所有的 SQL 连接和检索都是在一个单独的类中完成的,我在结果活动开始时调用该类。
MySQLRetrieve data = new MySQLRetrieve();
ArrayList<Tile> tiles = data.getResults(nameValuePairs, isLocationSearch);
上面的代码是我如何获取 SQL 响应并将其转换为 ArrayList,然后使用它填充我的 ListView。 getResults() 负责所有这些。
我已经有单独的线程用于将图像下载到 ListView 中,但我无法开始工作,让 SQL 查询和结果在它自己的线程中运行。我想要实现的是:
- 用户在搜索活动中输入搜索查询。
- Intent 被发送到结果 Activity,并立即启动。
- 当 SQL 查询发生时,会显示 ProgressDialog(只是动画旋转器,而不是加载栏)。
- ListView 使用 JSON 数组中的对象进行填充,并在图像出现时延迟加载图像。
我的步骤 1,2 和 4 运行良好,但 3 是问题。我查找了 AsyncTasks,这似乎是答案,但我就是无法让它们工作。有人有解决这个问题的办法吗?我需要这样做,所以当启动结果活动时,UI 立即更改为结果活动,而不必等到 SQL 响应返回。
是的,我已经阅读了无痛线程帖子。
谢谢。
I'm having issues with multithreading in my application. I know there are many posts on Threads/AsyncTasks/etc, but none seem to address my specific problem.
Basically, I get a query string in my search Activity, then send it to my results Activity, where the string is used as a SQL query, the results are returned as an array of JSON objects, then I display these objects in a ListView (which is part of the results Activity). All of my SQL connection and retrieval is done in a separate class that I call at the start of the results Activity.
MySQLRetrieve data = new MySQLRetrieve();
ArrayList<Tile> tiles = data.getResults(nameValuePairs, isLocationSearch);
The above code is how I get the SQL response and convert into an ArrayList, which I then use the populate my ListView with. getResults() takes care of all of this.
I already have separate threads working to download images into the ListView, but what I can't get to work is getting the SQL query and result to run in it's own Thread. What I want to achieve is this:
- User enters search query in search Activity.
- Intent is sent to results Activity, and it starts immediately.
- ProgressDialog (just the animated spinner thing, not a loading bar) displays while the SQL query is taking place.
- ListView populates with objects from the JSON array, lazy loading images as they come.
I have steps 1,2, and 4 working well, but 3 is the problem. I've looked up AsyncTasks, which seem to be the answer, but I just can't get them to work. Does anyone have a solution to this problem? I need to do this, so when starting the results Activity, the UI changes immediately to the results Activity and doesn't have to wait until the SQL response is returned.
And yes, I've already read the painless-threading post.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议不要创建 ArrayList以减少内存消耗(和代码大小),而是直接将 SQLite
Cursor
绑定到ListView
使用CursorAdapter。仅此一点就可能足以提高性能,以至于您不需要执行任何异步加载。
如果您仍然想要异步加载,请查看
LoaderManager
框架(自 Android 3.0/ API 级别 11 起可用,Android 支持包低至 1.6/4),它将自动异步加载您的光标
- 可以使用内置的CursorLoader
(如果您碰巧有一个ContentProvider
) , 或者由其他 SO 用户创建的SimpleCursorLoader
(如果您不这样做) t)。I would recommend against creating that
ArrayList<Tile>
to reduce memory consumption (and code size) and instead directly bind the SQLiteCursor
to theListView
using aCursorAdapter
.That alone might just increase the performance enough that you don't need to do any async loading.
If you still want async loading, check out the
LoaderManager
framework (available since Android 3.0/ API level 11, with Android support package down to 1.6/4) which will automagically do asynchronous loading of yourCursor
-- either using the built-inCursorLoader
(if you happen to have aContentProvider
), or theSimpleCursorLoader
created by a fellow SO user (if you don't).