我正在使用自定義的CursorAdapter從SQLite數據庫獲取數據並將其顯示在列表視圖中。數據庫包含大約8000行的2列。所以我正在尋找一種方法來儘可能快地查詢和顯示所有數據。我有做的AsyncTask這這裏是代碼:使用Cursor和ListView適配器處理大量數據
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();
}
}
,當我需要的結果集到一個適配器這部作品除了一部分好。我做了一些時間測試,它需要約700毫秒才能通過startManagingCursor。問題是大約需要7秒才能使其通過setAdapter(結果),並且這是在UI線程中運行的,所以它使我的應用程序無響應(進度對話框凍結,有時會執行應用程序)。我該如何減少這一次?我可以讓這也運行在後臺或以任何方式來提高響應能力?
tnx。
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());
}
}
}
用戶是否真的想滾動ListView中的8000行?有沒有辦法讓他們先過濾列表? – EboMike 2010-12-02 00:33:34
我有一個fastscroll與索引實施。我稍後會添加一個搜索選項來過濾。 – DArkO 2010-12-02 00:54:19