2013-08-28 82 views
0

我有一個令人沮喪的困難時間搞清楚這一點。我想我只是不理解如何正確組裝一個AsyncTask。我在我的類中有這個方法,但希望它作爲AsyncTask運行,而不是在調用它時將所有內容都停下來。誰能幫忙?提前致謝。將方法制作成AsyncTask的麻煩

private void getDatesNames() { 
JSONParser jParser = new JSONParser(); 

JSONObject json = jParser.getJSONFromUrl(url); 

ArrayList<String> dates = new ArrayList<String>(); 
ArrayList<String> teams = new ArrayList<String>(); 


try { 

    contacts = json.getJSONArray(TAG_CONTACTS); 


    for(int i = 0; i < contacts.length(); i++){ 
     JSONObject c = contacts.getJSONObject(i); 

     if ((c.getString(TAG_EMAIL1)).contains(league)) { 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String email1 = c.getString(TAG_EMAIL1); 
      String email2 = c.getString(TAG_EMAIL2); 


      dates.add(id); 
      teams.add(email1); 
      teams.add(email2); 
     } 
    } 

} catch (JSONException e) { 
    e.printStackTrace(); 
} 


LinkedHashSet hs1 = new LinkedHashSet(); 
LinkedHashSet hs2 = new LinkedHashSet(); 

hs1.addAll(dates); 
hs2.addAll(teams); 
dates.clear(); 
teams.clear(); 
dates.addAll(hs1); 
teams.addAll(hs2); 

    for (int i = 0; i < dates.size(); ++i) { 
    adapter1.add(dates.get(i)); 
} 

for (int i = 0; i < teams.size(); ++i) 
{ 

    adapter2.add(teams.get(i)); 

} 

回答

0

只需在doInBackground中粘貼您的緩慢的東西。如果你在填充兩個數組,那麼最好讓它們成爲最終的私有數據,所以不同的方面可以看到它們。

AsyncTask<Void, Void, Void> newTask = new AsyncTask<Void, Void, Void>() { 

     private final ArrayList<String> dates = new ArrayList<String>(); 
     private final ArrayList<String> teams = new ArrayList<String>(); 
     @Override 
     protected void onPreExecute() { 
      //You dont' really need anything here, so you could leave out onPreExecute()... 
     } 

     @Override 
     protected ArrayList<String> doInBackground(Void... params) { 
      JSONParser jParser = new JSONParser(); 

      JSONObject json = jParser.getJSONFromUrl(url); 

      try { 

       contacts = json.getJSONArray(TAG_CONTACTS); 

       for(int i = 0; i < contacts.length(); i++){ 
        JSONObject c = contacts.getJSONObject(i); 

        if ((c.getString(TAG_EMAIL1)).contains(league)) { 

         // Storing each json item in variable 
         String id = c.getString(TAG_ID); 
         String email1 = c.getString(TAG_EMAIL1); 
         String email2 = c.getString(TAG_EMAIL2); 


         dates.add(id); 
         teams.add(email1); 
         teams.add(email2); 
        } 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 


      LinkedHashSet hs1 = new LinkedHashSet(); 
      LinkedHashSet hs2 = new LinkedHashSet(); 

      hs1.addAll(dates); 
      hs2.addAll(teams); 
      dates.clear(); 
      teams.clear(); 
      dates.addAll(hs1); 
      teams.addAll(hs2); 

     } 

     @Override 
     protected void onPostExecute(ArrayList<String> fileList) { 
      // dismiss dialog if necessary 
      for (int i = 0; i < dates.size(); ++i) { 
       adapter1.add(dates.get(i)); 
      } 

      for (int i = 0; i < teams.size(); ++i) 
      { 

       adapter2.add(teams.get(i)); 

      } 
     } 

    }; 
    newTask.execute(); 
0

您需要在doInBackground(String... arg)中運行getDateNames()。看看this tutorial。它也適用於JSON並使用AsyncTasks。