Android swapCursor()slow:“;应用程序可能在其主线程上做了太多工作。”;
我一直看到“应用程序可能在其主线程上做了太多工作。” 这是由下面代码中的swapCursor()引起的吗?看起来是这样的:如果我删除它,上面的警告就会消失 我仍然不明白为什么它声称是导致问题的主线。我将swapCursor()移动到了不同的位置,例如onLoadFinished()和in loadInBackground(),但得到了相同的结果 如何调用swapCursor()以避免出现此警告?我使用SimpleCursorLoader的全部原因是为了避免挂起主线程,但它仍然在发生Android swapCursor()slow:“;应用程序可能在其主线程上做了太多工作。”;,android,android-listview,simplecursoradapter,android-cursorloader,android-loadermanager,Android,Android Listview,Simplecursoradapter,Android Cursorloader,Android Loadermanager,我一直看到“应用程序可能在其主线程上做了太多工作。” 这是由下面代码中的swapCursor()引起的吗?看起来是这样的:如果我删除它,上面的警告就会消失 我仍然不明白为什么它声称是导致问题的主线。我将swapCursor()移动到了不同的位置,例如onLoadFinished()和in loadInBackground(),但得到了相同的结果 如何调用swapCursor()以避免出现此警告?我使用SimpleCursorLoader的全部原因是为了避免挂起主线程,但它仍然在发生 packag
package com.example.sqlitetest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.CursorLoader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor>
{
public static Context mContext;
private LoaderManager.LoaderCallbacks<Cursor> mLoaderCallbacks;
public static SQLiteDatabase db;
public static SimpleCursorAdapter cAdapter;
public static ListView lvCustomList;
public static final class MyCursorLoader extends SimpleCursorLoader
{
public MyCursorLoader( Context context )
{
super( context );
}
@Override
public Cursor loadInBackground()
{
Cursor cursor = null;
cursor = db.rawQuery( "SELECT rowid _id, Name, Rating FROM Tune ORDER BY Name", null );
cAdapter.swapCursor( cursor );
return cursor;
}
}
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args)
{
return new MyCursorLoader( this );
}
@Override
public void onLoadFinished( Loader<Cursor> loader, Cursor cursor )
{
// cAdapter.swapCursor( cursor );
}
@Override
public void onLoaderReset( Loader<Cursor> loader )
{
cAdapter.swapCursor( null );
}
@Override
public void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
setContentView( R.layout.tune_artist_album_view );
mContext = this;
String path = "/sdcard/MyDb.sqlite";
db = SQLiteDatabase.openDatabase( path, null, 0 );
lvCustomList = (ListView) findViewById( R.id.lv_custom_list );
String[] columns = new String[] { "Name", "Rating" };
int[] to = new int[] { R.id.lv_tune };
cAdapter = new SimpleCursorAdapter( mContext, R.layout.like_hate_row, null, columns, to, 0 );
lvCustomList.setAdapter( cAdapter );
mLoaderCallbacks = this;
LoaderManager lm = getSupportLoaderManager();
lm.initLoader( 0, null, mLoaderCallbacks );
}
}
package com.example.sqlitetest;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.database.Cursor;
导入android.database.sqlite.SQLiteDatabase;
导入android.os.Bundle;
导入android.support.v4.app.FragmentActivity;
导入android.support.v4.app.LoaderManager;
导入android.support.v4.app.LoaderManager.LoaderCallbacks;
导入android.support.v4.content.Loader;
导入android.support.v4.content.CursorLoader;
导入android.support.v4.widget.SimpleCursorAdapter;
导入android.support.v4.widget.SimpleCursorAdapter.ViewBinder;
导入android.view.Gravity;
导入android.view.MotionEvent;
导入android.view.view;
导入android.view.view.OnTouchListener;
导入android.view.ViewGroup;
导入android.widget.AbsListView;
导入android.widget.AdapterView;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
导入android.widget.LinearLayout.LayoutParams;
导入android.widget.ListView;
导入android.widget.TextView;
公共类MainActivity扩展FragmentActivity实现LoaderManager.LoaderCallbacks
{
公共静态语境;
私有LoaderManager.LoaderCallbacks MLLoaderCallbacks;
公共静态数据库sqlitedb;
公共静态简易适配地籍仪;
公共静态列表视图lvCustomList;
公共静态最终类MyCursorLoader扩展了SimpleCursorLoader
{
公共MyCursorLoader(上下文)
{
超级(上下文);
}
@凌驾
公共游标loadInBackground()
{
游标=空;
cursor=db.rawQuery(“按名称从调谐顺序中选择rowid _id、名称、评级”,null);
地籍测量器(游标);
返回光标;
}
}
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args)
{
返回新的MyCursorLoader(此);
}
@凌驾
public void onLoadFinished(加载器,光标)
{
//地籍测量器(游标);
}
@凌驾
公共void onLoaderReset(加载器)
{
地籍登记簿(空);
}
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.tune\u artist\u album\u view);
mContext=这个;
字符串路径=“/sdcard/MyDb.sqlite”;
db=SQLiteDatabase.openDatabase(路径,null,0);
lvCustomList=(ListView)findViewById(R.id.lv\u custom\u list);
String[]columns=新字符串[]{“Name”,“Rating”};
int[]to=newint[]{R.id.lv_tune};
cAdapter=新的SimpleCorsorAdapter(mContext,R.layout.like_hate_row,null,columns,to,0);
lvCustomList.setAdapter(cAdapter);
mLoaderCallbacks=这个;
LoaderManager lm=getSupportLoaderManager();
lm.initLoader(0,null,mLoaderCallbacks);
}
}
下面是SimpleCursorLoader:
package com.example.sqlitetest;
import android.content.Context;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
/**
* Used to write apps that run on platforms prior to Android 3.0. When running
* on Android 3.0 or above, this implementation is still used; it does not try
* to switch to the framework's implementation. See the framework SDK
* documentation for a class overview.
*
* This was based on the CursorLoader class
*/
public abstract class SimpleCursorLoader extends AsyncTaskLoader<Cursor>
{
private Cursor mCursor;
public SimpleCursorLoader( Context context )
{
super( context );
}
/* Runs on a worker thread */
@Override
public abstract Cursor loadInBackground();
/* Runs on the UI thread */
@Override
public void deliverResult( Cursor cursor )
{
if( isReset() )
{
// An async query came in while the loader is stopped
if( cursor != null )
{
cursor.close();
}
return;
}
Cursor oldCursor = mCursor;
mCursor = cursor;
if( isStarted() )
{
super.deliverResult( cursor );
}
if( oldCursor != null && oldCursor != cursor && !oldCursor.isClosed() )
{
oldCursor.close();
}
}
/**
* Starts an asynchronous load of the contacts list data. When the result is
* ready the callbacks will be called on the UI thread. If a previous load has
* been completed and is still valid the result may be passed to the callbacks
* immediately.
* <p/>
* Must be called from the UI thread
*/
@Override
protected void onStartLoading()
{
if( mCursor != null )
{
deliverResult( mCursor );
}
if( takeContentChanged() || mCursor == null )
{
forceLoad();
}
}
/**
* Must be called from the UI thread
*/
@Override
protected void onStopLoading()
{
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
public void onCanceled( Cursor cursor )
{
if( cursor != null && !cursor.isClosed() )
{
cursor.close();
}
}
@Override
protected void onReset()
{
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if( mCursor != null && !mCursor.isClosed() )
{
mCursor.close();
}
mCursor = null;
}
}
package com.example.sqlitetest;
导入android.content.Context;
导入android.database.Cursor;
导入android.support.v4.content.AsyncTaskLoader;
/**
*用于编写在Android 3.0之前的平台上运行的应用程序。跑步时
*在Android 3.0或更高版本上,仍然使用此实现;它不尝试
*切换到框架的实现。请参阅框架SDK
*类概述的文档。
*
*这是基于CursorLoader类的
*/
公共抽象类SimpleCursorLoader扩展了AsyncTaskLoader
{
私有游标mCursor;
公共SimpleCursorLoader(上下文)
{
超级(上下文);
}
/*在工作线程上运行*/
@凌驾
公共摘要游标loadInBackground();
/*在UI线程上运行*/
@凌驾
公共无效交付结果(光标)
{
if(isReset())
{
//加载程序停止时出现异步查询
如果(光标!=null)
{
cursor.close();
}
返回;
}
游标oldCursor=mCursor;
mCursor=光标;
如果(isStarted())
{
super.deliverResult(光标);
}
if(oldCursor!=null&&oldCursor!=cursor&&oldCursor.isClosed())
{
oldCursor.close();
}
}
/**
*启动联系人列表数据的异步加载
*准备好,将在UI线程上调用回调
*已完成且仍然有效。结果可以传递给回调
*马上。
*
*必须从UI线程调用
*/
@凌驾
开始加载时受保护的void()
{
if(mCursor!=null)
{
交付结果(mCursor);
}
if(takeContentChanged()| | mCursor==null)
{
力载荷();
}
}
/**
*必须从UI线程调用
*/
@凌驾
受保护的无效onStopLoading()
{
//如果可能,尝试取消当前加载任务。
取消加载();
}
@凌驾
已取消的公共作废(光标)
{
if(cursor!=null&!cursor.isClosed())
{
cursor.close();
}
}
@凌驾
受保护的void onReset()
{
super.onReset();
//确保装载机已停止
onStopLoading();
if(mCursor!=null&&!mCursor.isClosed())
{
mCursor.close();
}
mCursor=null;
}
}
像query()
和rawQuery()
这样的方法不起作用