Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing Cursor with ListView adapter for a large amount of data
    text
    copied!<p>i am using a custom CursorAdapter to get data from a SQLite database and show it in a listview. The database contains a 2 columns with about 8.000 rows. so i am looking for a way to query and show all the data as fast as possible. i have done this with asyncTask here is the code:</p> <pre><code>private class PrepareAdapter extends AsyncTask&lt;Void,Void,CustomCursorAdapter &gt; { @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(); } </code></pre> <p>}</p> <p>this works good except the part when i need to set the result into an adapter. i have done some time tests and it takes aprox 700 ms to make it past the startManagingCursor. the problem is that it takes about 7 seconds to make it past the setAdapter(result) and this is running in UI thread so it makes my app unresponsive (the progress dialog freezes and sometimes does the app). how do i make this time less? can i make this also run in background or any way to increase responsiveness?</p> <p>tnx.</p> <pre><code>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&lt;?&gt; arg0, View arg1, int arg2, long arg3) { Log.e("item Click", arg1.toString()+ " position&gt; " +arg2); } @Override public void onClick(View v) { if(v.getId()==R.id.Button01){ //Log.e("Button Click", v.toString()+ " position&gt; " +v.getTag().toString()); v.setVisibility(View.INVISIBLE); DataBaseNamesHelper dbNames = new DataBaseNamesHelper(context); dbNames.setFavouritesFlag(v.getTag().toString()); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload