2013-01-14 207 views
2

我現在遇到了我的ArrayAdapter問題......我得到錯誤「構造函數ArrayAdapter(VideoPlay.ShowTitlesTask,int,int,ArrayList)未定義。」我究竟做錯了什麼?Android中的異步任務

的問題是在類ShowTitlesTask

package com.aer.illbehonest; 

import java.io.IOException; 
import java.io.InputStream; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
import java.util.ArrayList; 
import java.util.concurrent.ExecutionException; 

import net.sf.json.JSONArray; 
import net.sf.json.JSONObject; 
import net.sf.json.JSONSerializer; 

import org.apache.commons.io.IOUtils; 

import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 

public class VideoPlay extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_video_play); 

     new ShowTitlesTask().execute(""); 
    } 

    class ShowTitlesTask extends AsyncTask<String, Void, ArrayList<String>> { 

    ArrayList<String> titlesList; 

    @Override 
protected ArrayList<String> doInBackground(String... args) { 

    URL jsonURL = null; 
    try { 
      jsonURL = new URL("http://gdata.youtube.com/feeds/api/users/illbehonest/uploads?v=2&alt=jsonc"); 
     } catch (MalformedURLException e3) { 
      e3.printStackTrace(); 
     } 
      URLConnection jc = null; 
      try { 
       jc = jsonURL.openConnection(); 
      } catch (IOException e2) { 
       e2.printStackTrace(); 
      } 
      InputStream is = null; 
      try { 
       is = jc.getInputStream(); 
      } catch (IOException e1) { 
       e1.printStackTrace(); 
      } 
      String jsonTxt = null; 
      try { 
       jsonTxt = IOUtils.toString(is); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 

      JSONObject json = (JSONObject) JSONSerializer.toJSON(jsonTxt); 

      JSONObject jdata = json.getJSONObject("data"); 

      JSONArray jentry = jdata.getJSONArray("items"); 
      for (int entryNumber = 0; entryNumber<=25; entryNumber++){ 
        JSONObject entry = jentry.getJSONObject(entryNumber); 
        titlesList.add(entry.getString("title")); 
      } 

      return titlesList; 
    } 
    protected void onPostExecute(ArrayList<String> result) { 
     ListView listView = (ListView)VideoPlay.this.findViewById(R.id.videolist); 

     ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, 
       android.R.layout.simple_list_item_1, android.R.id.text1, titlesList); 
     titlesList.setAdapter(arrayAdapter); 
     } 
    } 
} 

回答

0

,你將需要從doInBackground返回一個ArrayList和onPostExecute因爲doInBackground執行完成時調用此方法添加到ArrayList中的ListView。創建您的AsyncTask爲:

private class GetTitlesTask extends AsyncTask<String, Void, ArrayList<String>> { 
    Activity activity; 
     ArrayList<String> titlesList; 
     public GetTitlesTask(Activity activity){ 
      this.activity=activity; 
     } 
      @Override 
      protected void onPreExecute() { 
      // show progress bar here 
      } 
     @Override 
     protected ArrayList<String> doInBackground(String... args) { 

      titlesList=new ArrayList<String>; 

       // get titles here and add to titlesList ArrayList 

      return titlesList; //<< return titlesList ArrayList 
     } 
     @Override 
     protected void onPostExecute(ArrayList<String> result) { 

      // set result ArrayList as datasouce to Adapter here 
      ListView listview= 
       (ListView)activity.findViewById(R.id.videolist); 
     } 
    } 

,並從UI作爲啓動該GetTitlesTask的AsyncTask:

new GetTitlesTask(Your_Current_Activity.this).execute(""); 

到更多的瞭解的AsyncTask看到

http://developer.android.com/reference/android/os/AsyncTask.html

+0

當我嘗試的ListView列表視圖設置爲findViewById(R.id.videolist)我得到一個錯誤說 「的方法findViewById(INT)是未定義的類型ShowTitlesTask」 –

+0

@ZacCanoy:確保'ShowTitlesTask'是一個Activity的內部類,如果是,則使用ListView listView =(ListView)your_Activity.this.findViewById(R.id.videolist);如果ShowTitlesTask不是Activity的內部類,則需要傳遞Activity的Context非活動類 –

+0

你是什麼意思的內部類?我是初學者。我試圖「按原樣」,它給了我「沒有封閉類型VideoPlay的實例**(我的活動)**可以在範圍內訪問」 –

0

當您使用AsyncTask你應該從onPostExecute方法更新您的用戶界面。在這裏設置適配器,然後就緒。

protected void onPostExecute(Void result) { 
titlesList.setAdapter(arrayAdapter);      
};