Android中的异步任务



我有麻烦与我的ArrayAdapter现在…我得到的错误"构造器ArrayAdapter(VideoPlay。showtitletask, int, int, ArrayList)未定义。我做错了什么?

问题出在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);
        }
    }
}

你需要从doInBackground返回一个ArrayList,并在onPostExecute中将ArrayList添加到ListView中,因为这个方法在doInBackground执行完成时调用。创建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

当你使用AsyncTask时,你应该从onPostExecute方法更新你的ui。把适配器设置在这里,你就准备好了。

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

相关内容

  • 没有找到相关文章

最新更新