android-处理 CursorAdapter getView() 中的查询问题

问题描述

处理 CursorAdapter getView() 中的查询问题
上面是getView()里的代码,db.isRegistered(contactId_text)方法是一个查询语句,需要一些时间来获取结果,所以就落后于列表视图滚动屏幕,有什么方法来处理这个问题呢?我使用AsyncTask,但是当列表太长时,就会获得 AsyncTask limit error。

public View getView(int position View convertView ViewGroup parent) {...        boolean flag = db.isRegistered(contactId_text);            ImageView iv = (ImageView) vi.findViewById(R.id.typeImage);        if (flag) {                holder.image1.setImageResource(R.drawable.registered);        }...}

解决方案

http://stackoverflow.com/questions/9654148/android-asynctask-threads-limits
All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and a LinkedBlockingQueue. When you call execute on an AsyncTask the ThreadPoolExecutor will execute it when it is ready some time in the future.

The 'when am I ready?' behaviour of a ThreadPoolExecutor is controlled by two parameters the core pool size and the maximum pool size. If there are less than core pool size threads currently active and a new job comes in the executor will create a new thread and execute it immediately. If there are at least core pool size threads running it will try to queue the job and wait until there is an idle thread available (i.e. until another job is completed). If it is not possible to queue the job (the queue can have a max capacity) it will create a new thread (upto maximum pool size threads) for the jobs to run in. Non-core idle threads can eventually be decommissioned according to a keep-alive timeout parameter.

Before Android 1.6 the core pool size was 1 and the maximum pool size was 10. Since Android 1.6 the core pore size is 5 and the maximum pool size is 128. The size of the queue is 10 in both cases. The keep-alive timeout was 10 seconds before 2.3 and 1 second since then.

With all of this in mind it now becomes clear why the AsyncTask will only appear to execute 5/6 of your tasks. The 6th task is being queued up until one of the other tasks complete. This is a very good reason why you should not use AsyncTasks for long-running operations - it will prevent other AsyncTasks from ever running.

For completeness if you repeated your exercise with more than 6 tasks (e.g. 30) you will see that more than 6 will enter doInBackground as the queue will become full and the executor is pushed to create more worker threads. If you kept with the long-running task you should see that 20/30 become active with 10 still in the queue.

时间: 2024-07-30 21:42:28

android-处理 CursorAdapter getView() 中的查询问题的相关文章

getview()-android getView中 position=0多次出现的解决办法

问题描述 android getView中 position=0多次出现的解决办法 适配器中的getview @Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(context,R.layout.home_item,null); TextView tv = (TextView) view.findViewById(R.id.homeIt

Android: Gallery的adapter中getView方法被执行多次

项目中遇到一个奇怪的问题:为Gallery设置的adapter中的getView方法被调用多次 客户需求:一次只能滑动一格 参考资料: http://stackoverflow.com/questions/4687370/gallery-scroll-one-image-at-a-time http://stackoverflow.com/questions/6058609/android-gallerys-getview-returning-incorrect-position Android

Android编程实现通讯录中联系人的读取,查询,添加功能示例

本文实例讲述了Android编程实现通讯录中联系人的读取,查询,添加功能.分享给大家供大家参考,具体如下: 先加二个读和写权限: <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> 具体代码: package com.ebo

jsp问题-jsp中模糊查询,后台没有出错,为什么实现不了?

问题描述 jsp中模糊查询,后台没有出错,为什么实现不了? 点击查找只是刷新页面,后台没有问题,但是又实现不了 具体代码: <% request.setCharacterEncoding("utf-8"); %> <%! private static final String DBDRIVER ="org.gjt.mm.mysql.Driver"; private static final String DBURL ="jdbc:mysq

Android ListView滑动过程中图片显示重复错乱闪烁的原因及解决方法

主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及ListView的缓存机制. 1.原因分析 ListView item缓存机制:为了使得性能更优,ListView会缓存行item(某行对应的View).ListView通过adapter的getView函数获得每行的item.滑动过程中, a. 如果某行item已经滑出屏幕,若该item不在缓存内,则put进缓存,否则更新缓存: b. 获取滑入屏幕的行item之前会先判断缓存中是否有可用的ite

Android ListView滑动过程中图片显示重复错位闪烁问题解决

主要分析Android ListView滚动过程中图片显示重复.错乱.闪烁的原因及解决方法,顺带提及ListView的缓存机制.1.原因分析ListView item缓存机制:为了使得性能更优,ListView会缓存行item(某行对应的View).ListView通过adapter的getView函数获得每行的item.滑动过程中, a. 如果某行item已经滑出屏幕,若该item不在缓存内,则put进缓存,否则更新缓存: b. 获取滑入屏幕的行item之前会先判断缓存中是否有可用的item,

android在异步任务中关闭Cursor的代码方法_Android

查询数据会比较耗时,所以我们想把查询数据放在一个异步任务中,查询结果获得Cursor,然后在onPostExecute (Cursor result)方法中设置Adapter,我们可能会想到使用Activity的managedQuery来生成Cursor,这样Cursor就会与Acitivity的生命周期一致了,多么完美的解决方法!然而事实上managedQuery也有很大的局限性,managedQuery生成的Cursor必须确保不会被替换,因为可能很多程序事实上查询条件都是不确定的,因此我们

android下载本地服务器中的文件

问题描述 android下载本地服务器中的文件 android客户端上传文件到本地服务器,把文件的相对路径存到数据库中,要下载该文件时,下载文件的url是什么,服务器端和客户端该怎么写服务器端是用servlet写的 解决方案 首先在电脑上配置tomcat,webapps目录下新建mp3文件夹, 截图 首先我们通过SAX解析resources.xml 获取歌曲信息. 启动 Tomcat访问一下服务器地址http://210.**.**.**:8080/mp3/resources.xml 21...

ListView中getView中放置多个item和getItemViewType的用法

istView 和 Adapter 的基础 工作原理: ListView 针对List中每个item,要求 adapter "给我一个视图" (getView). 一个新的视图被返回并显示 如果我们有上亿个项目要显示怎么办?为每个项目创建一个新视图?NO!这不可能! 实际上Android为你缓存了视图. Android中有个叫做Recycler的构件,下图是他的工作原理: 如果你有10亿个项目(item),其中只有可见的项目存在内存中,其他的在Recycler中. ListView先请