我希望找到一种解决方案来加快更新我的微调器。目前,每当我根据之前选择的微调器更改搜索条件时,我正在使用SimpleCursorAdapter并调用ChangeCursor。
我进行了一些时序测试,查询时间为5ms到60ms,而changeCursor函数需要600ms到4000ms +。是否有另一种更快速更新适配器上光标的方法?我没有使用相同的查询,所以我不能简单地重新查询游标,然后调用notifydatasetchanged。我必须创建一个新的查询,然后返回一个新的光标(也许有更好的方法来做这个部分)。
以下是我目前正在填充的方式
private void writerSpinner() {
String[] columns = new String[] { Passage.COL_WRITER_ID + " " + BaseColumns._ID, Passage.COL_WRITER_NAME };
String whereClause = null;
String groupBy = null;
String orderBy = Passage.COL_WRITER_ID + " ASC";
if (mAdapterPassage == null) {
String[] columnsSpinner = new String[] { Passage.COL_WRITER_NAME };
int[] to = new int[] { android.R.id.text1 };
mAdapterPassage = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, null, columnsSpinner, to);
mAdapterPassage.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mPassage.setAdapter(mAdapterPassage);
mPassage.setOnItemSelectedListener(onItemSelectedListener);
}
AsyncLoadData loadData = new AsyncLoadData(mAdapterPassage, mPassage, Passage.TABLE_NAME_WRITERS, columns, whereClause, groupBy, orderBy);
loadData.execute();
}
private void updateChapterSpinner() {
String[] columns = new String[] { Passage.COL_WRITER_ID + " " + BaseColumns._ID, Passage.COL_CHAPTER_ID };
String whereClause = Passage.COL_WRITER_ID + " = " + mSelectedWriterId;
String groupBy = Passage.COL_CHAPTER_ID;
String orderBy = Passage.COL_CHAPTER_ID + " ASC";
if (mAdapterChapter == null) {
String[] columnsSpinner = new String[] { Passage.COL_CHAPTER_ID };
int[] to = new int[] { android.R.id.text1 };
mAdapterChapter = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, null, columnsSpinner, to);
mAdapterChapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mChapter.setAdapter(mAdapterChapter);
mChapter.setOnItemSelectedListener(onItemSelectedListener);
}
AsyncLoadData loadData = new AsyncLoadData(mAdapterChapter, mChapter, Passage.TABLE_NAME_PASSAGES, columns, whereClause, groupBy, orderBy);
loadData.execute();
}
private void updateVerseSpinner() {
String[] columns = new String[] { Passage.COL_WRITER_ID + " " + BaseColumns._ID, Passage.COL_VERSE_ID };
String whereClause = Passage.COL_WRITER_ID + " = " + mSelectedWriterId
+ " AND " + Passage.COL_CHAPTER_ID + " = " + mSelectedChapter;
String groupBy = Passage.COL_VERSE_ID;
String orderBy = Passage.COL_VERSE_ID + " ASC";
if (mAdapterVerse == null) {
String[] columnsSpinner = new String[] { Passage.COL_VERSE_ID };
int[] to = new int[] { android.R.id.text1 };
mAdapterVerse = new SimpleCursorAdapter(getActivity(), android.R.layout.simple_spinner_item, null, columnsSpinner, to);
mAdapterVerse.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mVerse.setAdapter(mAdapterVerse);
mVerse.setOnItemSelectedListener(onItemSelectedListener);
}
AsyncLoadData loadData = new AsyncLoadData(mAdapterVerse, mVerse, Passage.TABLE_NAME_PASSAGES, columns, whereClause, groupBy, orderBy);
loadData.execute();
}
private class AsyncLoadData extends AsyncTask<Void, Void, Void> {
String mTableName;
String[] mColumns;
String mWhereClause;
String mGroupBy;
String mOrderBy;
Spinner mSpinner;
Cursor mCursor;
SimpleCursorAdapter mAdapter;
public AsyncLoadData(SimpleCursorAdapter adapter, Spinner spinner, String tableName, String[] columns, String whereClause, String groupBy, String orderBy) {
mAdapter = adapter;
mSpinner = spinner;
mTableName = tableName;
mColumns = columns;
mWhereClause = whereClause;
mGroupBy = groupBy;
mOrderBy = orderBy;
}
@Override
protected void onPreExecute()
{
//mSpinner.setVisibility(View.GONE);
}
@Override
protected Void doInBackground(Void... arg0) {
long startCursor = new Date().getTime();
mCursor = mDBHandler.query(mTableName, mColumns, mWhereClause, null, mGroupBy, null, mOrderBy);
long timeToQuery = new Date().getTime() - startCursor;
Log.i("CursorQuery", "Time to Query Cursor " + mGroupBy + ": " + timeToQuery + "ms");
return null;
}
@Override
protected void onPostExecute(Void result)
{
long startAdapter = new Date().getTime();
mAdapter.changeCursor(mCursor);
long timeToChangeCursor = new Date().getTime() - startAdapter;
Log.i("AdapterQuery", "Time to Change Cursor " + mGroupBy + ": " + timeToChangeCursor + "ms");
mAdapter.notifyDataSetChanged();
//mSpinner.setVisibility(View.VISIBLE);
}
}
private OnItemSelectedListener onItemSelectedListener = new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()) {
case R.id.bible_passage:
mSelectedWriterId = position + 1;
updateChapterSpinner();
break;
case R.id.bible_chapter:
mSelectedChapter = position + 1;
updateVerseSpinner();
break;
case R.id.bible_verse:
mSelectedVerse = position + 1;
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
答案 0 :(得分:3)
changeCursor
花费这么长时间的原因是返回的游标
Database.query(...)
仅在必要时初始化(所以它是
光标的故障,而不是适配器的故障。
光标初始化速度慢的原因是它解析了 立即整个结果集(因为它需要知道总计数)。 尽管SQLite非常快地返回大量查询,但java非常慢 解析它们(比SQLite慢100到1000倍)。
进来,HugeSQLiteCursor(with JavaDoc!)。 我写了这个光标来解决这个问题。它加载结果 递增和自动,而不是一次全部(聪明的 解决方法,找出总计数近乎0的开销。)
Download the .jar
file,
将其添加到您的libs/
文件夹,并执行以下操作
使用它(完全限定名称是com.malabarba.hugesqlitecursor.HugeSQLiteCursor
):
adapter.changeCursor(new HugeSQLiteCursor(db, stepSize,
table, columnFields,
selection, selectionArgs));
正如您所看到的,因为结果是懒惰加载的,您需要 将数据库和查询参数传递给构造函数。 一旦创建,光标应该在任何情况下完美地工作 之前使用的是SQLiteCursor。
第二个参数(stepSize
)是您想要的项目数
最初在创建光标时加载。 这很重要
参数即可。
整个游标的初始化时间将是有效的
就好像它只有stepSize
个结果一样。但是不要让stepSize
小于适合一个屏幕的结果数。
这个Cursor有效地消除了“Java太慢”的瓶颈。 在我的Nexus 4上,它返回50.000结果的查询,小于 60ms ,而SQLiteCursor花费了大约1000ms。 当然,如果你的查询非常庞大,即使SQLite返回结果的速度也很慢,那么这个游标只会让你到目前为止。