사용자 지정 CursorAdapter를 사용하여 SQLite 데이터베이스에서 데이터를 가져 와서 listview에 표시하고 있습니다. 데이터베이스에는 약 8.000 개의 행이있는 2 개의 열이 있습니다. 그래서 나는 가능한 한 빨리 쿼리하고 모든 데이터를 보여주는 방법을 찾고있다. 여기 AsyncTask를 함께이 작업을 수행 한 코드는 다음과 같습니다많은 양의 데이터에 대해 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();
}
}
내가 어댑터에 결과를 설정해야 할 때이 부분을 제외하고 잘 작동합니다. 나는 약간의 시간 테스트를했으며, startManagingCursor를 지나치게 만들기 위해 aprox 700ms가 걸린다. 문제는 setAdapter (결과)를 지나치는 데 약 7 초가 걸리고 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
인덱싱이 구현 된 빠른 스크롤이 있습니다. 그리고 나는 나중에 필터하기 위해 검색 옵션을 추가 할 것이다. – DArkO
어쨌든 별도의 스레드에서 쿼리를 수행 한 다음 어댑터에 할당 할 수 있지만 여전히 7 초 동안 앱을 검게 남겨 둡니다. 7 초 동안 사용자를 즐겁게하는 방법이 없다면? – EboMike