0

我现在无法将适配器分配给titlesList...我收到错误“方法setAdapter(ArrayAdapter)未定义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> (VideoPlay.this,
                android.R.layout.simple_list_item_1, android.R.id.text1, titlesList);
        titlesList.setAdapter(arrayAdapter);
        }
    }
}
4

1 回答 1

1

this指向您的 AsyncTask 而不是 Activity (上下文)。尝试VideoPlay.this

于 2013-01-14T21:27:17.517 回答