Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用游标避免ANR_Android_Sqlite_Cursor - Fatal编程技术网

Android 使用游标避免ANR

Android 使用游标避免ANR,android,sqlite,cursor,Android,Sqlite,Cursor,我有一个活动,它在Sqlite DB上运行查询,获取一个游标,使用该游标创建一个CustomCursorAdapter,并将其附加到活动中的ListView。看起来是这样的: SQLiteDatabase db=new StuffHelper(this).getWritableDatabase(); Cursor c1=db.query(StuffHelper.TABLE, new String[]{StuffHelper._ID}, StuffH

我有一个活动,它在Sqlite DB上运行查询,获取一个游标,使用该游标创建一个CustomCursorAdapter,并将其附加到活动中的ListView。看起来是这样的:

SQLiteDatabase db=new StuffHelper(this).getWritableDatabase();
Cursor c1=db.query(StuffHelper.TABLE,
            new String[]{StuffHelper._ID},
            StuffHelper.SIZE+">=?",
            new String[]{"64"},
            null,
            null,
            null);
startManagingCursor(c1);

StuffAdapter a1 = new StuffAdapter(this, c1);

ListView ll1 = (ListView) findViewById(R.id.ll1);
ll1.setAdapter(a1);

就ANR而言,当前设置是否存在问题?当使用游标时,我如何告诉android在后台线程上运行所有Sqlite内容?

您没有给出运行此代码的上下文,但无论如何我会咬一口


是的,它确实有发生ANR的风险。它还面临各种其他生命周期问题的风险。由于
setListAdapter()
需要在通常在
onCreate()
中执行的各种其他操作之前进行调用,因此您可能希望将数据库访问卸载到一个单独的线程(如异步任务),该线程可以根据需要进行调用/缓存/管理。AsyncTask在线程启动前提供基于UI的回调,在线程结束时提供基于UI的回调。ListAdapter可以在不引用游标的情况下创建和分配(我建议您尽快解决这个问题……使用自定义列表适配器似乎没有什么好的理由,您应该更好地管理数据库访问)


在活动拆卸和重建过程中管理这项任务(想想改变方向…)是一个完全不同的过程,并且已经被描述得非常恶心了。

您没有给出运行这段代码的太多上下文,但我还是要咬一口


是的,它确实有发生ANR的风险。它还面临各种其他生命周期问题的风险。由于
setListAdapter()
需要在通常在
onCreate()
中执行的各种其他操作之前进行调用,因此您可能希望将数据库访问卸载到一个单独的线程(如异步任务),该线程可以根据需要进行调用/缓存/管理。AsyncTask在线程启动前提供基于UI的回调,在线程结束时提供基于UI的回调。ListAdapter可以在不引用游标的情况下创建和分配(我建议您尽快解决这个问题……使用自定义列表适配器似乎没有什么好的理由,您应该更好地管理数据库访问)


在活动拆卸和重建过程中管理此任务(考虑改变方向…)是一个完全不同的过程,因此已被详细介绍。

请将您的UI与后台任务分开。将光标部分写在后台,这样就可以显示任何UI或进度对话框。为此使用异步任务

AsyncTask类具有以下方法

1. doInBackground: Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.
   2. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
   3. onPreExecute: This method is called before doInBackground method is called.
   4. onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.

谢谢
Deepak

请将您的UI与后台任务分开。将光标部分写在后台,这样就可以显示任何UI或进度对话框。为此使用异步任务

AsyncTask类具有以下方法

1. doInBackground: Code performing long running operation goes in this method.  When onClick method is executed on click of button, it calls execute method which accepts parameters and automatically calls doInBackground method with the parameters passed.
   2. onPostExecute: This method is called after doInBackground method completes processing. Result from doInBackground is passed to this method.
   3. onPreExecute: This method is called before doInBackground method is called.
   4. onProgressUpdate: This method is invoked by calling publishProgress anytime from doInBackground call this method.

谢谢
Deepak

“可以创建和分配ListAdapter,而无需对游标进行任何引用(我建议您尽快修复该问题)。如果您查看类似SimpleCursorAdapter的内容,则需要在构造函数中使用游标。Hmm:
m_ListAdapter=new ArrayAdapter((上下文)this,R.layout.list_row,R.id.rowText,m_arrayList){@Override public View getView(int位置、视图转换视图、视图组父级){…}setListAdapter(m_listAdapter);
格式不好,但是,嘿,我分配了一个列表适配器,但从来没有做过游标。使用
SimpleCorsOrAdapter
应该被严重视为Android反模式,并且受到所有教程和示例代码的攻击。@Chris是的,但是你使用的ArrayAdapter不能与游标一起工作。这就是为什么我需要一个游标适配器才能工作的原因使用DB。听起来不错:所以基本上我在后台线程中循环游标并创建一个ArrayList?“可以创建和分配ListAdapter,而无需对游标进行任何引用(我建议您尽快修复该问题)”。如果您查看类似SimpleCursorAdapter的内容,它需要构造函数中有一个光标。Hmm:
m_listAdapter=new ArrayAdapter((上下文)this,R.layout.list_row,R.id.rowText,m_arrayList){@Override public View getView(int position,View convertView,ViewGroup parent){…}setListAdapter(m_listAdapter);
格式不好,但是,嘿,我分配了一个列表适配器,但从来没有做过游标。使用
SimpleCorsOrAdapter
应该被严重视为Android反模式,并且受到所有教程和示例代码的攻击。@Chris是的,但是你使用的ArrayAdapter不能与游标一起工作。这就是为什么我需要一个游标适配器才能工作的原因使用DB。听起来不错:所以基本上我在后台线程中循环光标并创建一个ArrayList?好的:所以在AsyncTask中获得光标后,我只将其应用于listview?调用
m_listAdapter.notifyDataSetChanged();
当支持ListAdapter的数据发生更改时。通常,这会出现在类似于您的
onPostExecute()的内容上
方法。如果您有一个足够长的查询,而您实际上担心这种情况,我强烈建议您多读一些内容。好的:因此,在AsyncTask中获得光标后,我将其应用于listview?Call
m_listAdapter.notifyDataSetChanged()当支持ListAdapter的数据发生更改时。。通常,这会出现在AyncTask中的
onPostExecute()
方法上。如果您有足够长的查询,而您实际上担心这种情况,我强烈建议您多读一些。