-1

如何在asyncTask中使用Cursor来显示listView,因为我的抓取数据非常庞大(元数据),我应该使用asyncTask。我需要链接教程使用CursorasyncTask如何在asyncTask中使用Cursor获取大量数据?

我有波纹管的代码。

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

请解释您的意思是“巨大的”。 – Squonk 2014-12-03 13:44:10

+0

@Squonk。元数据。 – 2014-12-03 13:45:21

+0

你是什么意思?只需在任务内执行查询。 – TheRedFox 2014-12-03 13:47:02

回答

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(); 
+0

AsyncTask不适合那种需求@ A.A需要..它AsyncTask适合短操作 – 2014-12-03 13:58:13