我正在使用自定义CursorAdapter从SQLite数据库获取数据并在ListView中显示。该数据库包含大约8000行的2列。因此,我正在寻找一种尽可能快地查询和显示所有数据的方法。我已经使用asyncTask完成了这个操作,以下是代码:
private class PrepareAdapter extends AsyncTask<Void,Void,CustomCursorAdapter > {
@Override
protected void onPreExecute() {
dialog.setMessage("Wait");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
dialog.show();
Log.e("TAG","Posle nov mAdapter");
}
@Override
protected CustomCursorAdapter doInBackground(Void... unused) {
Cursor cursor = myDbNamesHelper.getCursorQueryWithAllTheData();
mAdapter.changeCursor(cursor);
startManagingCursor(cursor);
Log.e("TIME","posle start managing Cursor" + String.valueOf(SystemClock.elapsedRealtime()-testTime)+ " ms");
testTime=SystemClock.elapsedRealtime();
mAdapter.initIndexer(cursor);
return mAdapter;
}
protected void onPostExecute(CustomCursorAdapter result) {
TabFirstView.this.getListView().setAdapter(result);
Log.e("TIME","posle adapterSet" + String.valueOf(SystemClock.elapsedRealtime()-testTime)+ " ms");
testTime=SystemClock.elapsedRealtime();
dialog.dismiss();
}
这个方法很好,但是当我需要将结果设置到适配器中时出现了问题。我进行了一些时间测试,发现经过startManagingCursor大约需要700毫秒,但经过setAdapter(result)需要大约7秒钟。由于这是在UI线程中运行的,因此会导致我的应用程序不响应(进度对话框冻结并且有时应用程序也会冻结)。我该如何减少这段时间?我能否使其在后台运行或以任何方式提高响应性?
谢谢。
public class CustomCursorAdapter extends SimpleCursorAdapter implements OnClickListener,SectionIndexer,Filterable,
android.widget.AdapterView.OnItemClickListener{
private Context context;
private int layout;
private AlphabetIndexer alphaIndexer;
public CustomCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) {
super(context, layout, c, from, to);
this.context = context;
this.layout = layout;
}
public void initIndexer(Cursor c){
alphaIndexer=new AlphabetIndexer(c, c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME), " ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Cursor c = getCursor();
final LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(layout, parent, false);
int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED);
int fav = c.getInt(favCol);
int idCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_ID);
Button button = (Button) v.findViewById(R.id.Button01);
button.setOnClickListener(this);
button.setTag(c.getInt(idCol));
if(fav==1){
button.setVisibility(View.INVISIBLE);
}
else button.setVisibility(View.VISIBLE);
return v;
}
@Override
public void bindView(View v, Context context, Cursor c) {
int nameCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_NAME);
String name = c.getString(nameCol);
/**
* Next set the name of the entry.
*/
TextView name_text = (TextView) v.findViewById(R.id.name_entry);
if (name_text != null) {
name_text.setText(name);
}
int favCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_FAVOURITED);
int fav = c.getInt(favCol);
Button button = (Button) v.findViewById(R.id.Button01);
button.setOnClickListener(this);
int idCol = c.getColumnIndex(DataBaseNamesHelper.COLUMN_ID);
button.setTag(c.getInt(idCol));
// Log.e("fav",String.valueOf(fav));
if(fav==1){
button.setVisibility(View.INVISIBLE);
} else button.setVisibility(View.VISIBLE);
}
@Override
public int getPositionForSection(int section) {
return alphaIndexer.getPositionForSection(section);
}
@Override
public int getSectionForPosition(int position) {
return alphaIndexer.getSectionForPosition(position);
}
@Override
public Object[] getSections() {
return alphaIndexer.getSections();
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Log.e("item Click", arg1.toString()+ " position> " +arg2);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.Button01){
//Log.e("Button Click", v.toString()+ " position> " +v.getTag().toString());
v.setVisibility(View.INVISIBLE);
DataBaseNamesHelper dbNames = new DataBaseNamesHelper(context);
dbNames.setFavouritesFlag(v.getTag().toString());
}
}
}