【问题标题】:How to use Cursor in asyncTask for fetch huge data?如何在 asyncTask 中使用 Cursor 获取大量数据?
【发布时间】:2014-12-03 13:40:12
【问题描述】:

我如何在asyncTask 中使用CursorlistView 中显示,因为我的获取数据很大(元数据),我应该从asyncTask 中使用。我需要一个链接以供教程使用CursorasyncTask.

我有下面的代码。

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

【问题讨论】:

  • 解释一下你所说的“巨大”是什么意思。
  • 什么意思?只需在任务中执行查询即可。
  • 给我链接在 asyncTask 中使用光标。

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


【解决方案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();

【讨论】:

  • AsyncTask 不适合 @A.A 想要的那种要求.. 它 AsyncTask 适合短期操作
【解决方案2】:

使用 AsyncTaskLoader 类

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

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

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

这是一个关于上面所说的整个事情的很棒的教程

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    相关资源
    最近更新 更多