如何在asyncTask中使用Cursor获取大数据?

时间:2014-12-03 13:40:12

标签: java android android-listview android-asynctask android-cursor

我如何在Cursor中使用asyncTask进行listView中的show,因为我的fetch数据很大(元数据),我应该使用asyncTask。我需要一个链接教程使用Cursor中的asyncTask

我有下面的代码。

我的Struct_Search.class

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

在我的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();
    }

2 个答案:

答案 0 :(得分:1)

使用AsyncTaskLoader类

http://developer.android.com/reference/android/content/AsyncTaskLoader.html

你基本上必须使用LoaderManager Framework来完成你的任务

http://developer.android.com/reference/android/app/LoaderManager.LoaderCallbacks.html

这是关于整个事情的一个很好的教程,如上所述

http://www.androiddesignpatterns.com/2012/07/understanding-loadermanager.html

答案 1 :(得分:1)

这是我之前使用的模式:

 //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();
            }

        }

这个(下面)应该在你的onCreate /某处你想要执行异步任务的地方调用。

new AsyncCaller().execute();