Android中的异步任务

时间:2013-01-14 05:01:54

标签: android android-listview android-asynctask arrays

我现在遇到了我的ArrayAdapter问题...我收到错误“构造函数ArrayAdapter(VideoPlay.ShowTitlesTask,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);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你需要从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

答案 1 :(得分:0)

在使用AsyncTask时,您应该使用onPostExecute方法更新您的ui。在这里设置适配器就可以了。

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