Search code examples
javaandroidandroid-listviewandroid-asynctaskandroid-cursor

How to use Cursor in asyncTask for fetch huge data?


How I can use Cursor in asyncTask for show in listView , because my data for fetch is huge(metadata) and I should use from asyncTask .I need to a link for tutorial use Cursor in asyncTask.

I have bellow code .

My Struct_Search.class :

public class Struct_Search {
    public int MetaData;
    public String Value;
    public String Name;
    public int Number;
}

And in my MainActivity.class :

    try {
        cursor = sql.rawQuery(
                "SELECT MetaDataID,Data,CategoryID,ParentID FROM BOOK WHERE DATA LIKE '"
                        + "%" + editable + "%'", null);
        array = new ArrayList<String>();
        if (cursor != null) {
            if (cursor.moveToFirst()) {
                do {
                    Struct_Search note = new Struct_Search();
                    note.MetaData = cursor.getInt(cursor.getColumnIndex("MetaDataID"));
                    note.Value = cursor.getString(cursor.getColumnIndex("DATA"));
                    note.Number = cursor.getInt(cursor.getColumnIndex("CategoryID"));
                    ParentID = cursor.getInt(cursor.getColumnIndex("ParentID"));
                    CursorSecond = sql.rawQuery("SELECT name FROM ContentList WHERE id ="+ ParentID, null);
                    if (CursorSecond != null) {
                        do {
                            CursorSecond.moveToFirst();
                            note.NameSureh = CursorSecond.getString(CursorSecond.getColumnIndex("name"));
                            CursorSecond.close();
                        } while (CursorSecond.moveToNext());
                    }
                    notes.add(note);
                } while (cursor.moveToNext());
            }
            adapter.notifyDataSetChanged();
        }
    } catch (Exception e) {
    } finally {
        cursor.close();
    }

Solution

  • Here's a pattern I've used before:

     //Async caller for threading
            class AsyncCaller extends AsyncTask<Void, Void, Void>
            {
    
                public AsyncCaller()
                {
                   //initialize anything you may need here
                }
    
                ProgressDialog pdLoading = new ProgressDialog(/*Application Context here*/);
                @Override
                protected void onPreExecute() {
                    super.onPreExecute();
    
                    //this method will be running on UI thread, so change any UI here
                    pdLoading.setMessage("Set loading message here");
    
                    pdLoading.show();
                }
                @Override
                protected Void doInBackground(Void... params) {
    
                    //this method will be running on background thread so don't update UI frome here
                    //do your long running tasks here
    
    
                    return null;
                }
    
                @Override
                protected void onPostExecute(Void result) {
                    super.onPostExecute(result);
                    //put anything you need after execution here. 
    
                    pdLoading.dismiss();
                }
    
            }
    

    And this (below) should be called somewhere in your onCreate/where ever you want the async task to execute.

    new AsyncCaller().execute();