Android 内部类 AsyncTask 返回空 ArrayList



我试图返回通过解析从 AsyncTask 派生的内部类中的 XML 获得的 ArrayList,但是即使我在上层类中声明 ArrayList 变量馈送,系统也会给我一个空的 ArrayList。

public class ListFeed extends ListActivity {
public static final String WIFI = "Wi-Fi";
public static final String ANY = "Any";
private static final String URL = "http://stackoverflow.com/feeds/tag?tagnames=android&sort=newest";
// Whether there is a Wi-Fi connection.
private static boolean wifiConnected = false;
// Whether there is a mobile connection.
private static boolean mobileConnected = false;
// Whether the display should be refreshed.
public static boolean refreshDisplay = true;
public static String sPref = null;
private ArrayList feed = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_list_feed);
    new DownloadXmlTask().execute(URL);
    Log.d("feed size", Integer.toString(feed.size()));

    ListView listView = getListView();
    setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_list_feed,feed));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
    URL url = new URL(urlString);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000 /* milliseconds */);
    conn.setConnectTimeout(15000 /* milliseconds */);
    conn.setRequestMethod("GET");
    conn.setDoInput(true);
    // Starts the query
    conn.connect();
    return conn.getInputStream();
}
// Uploads XML from stackoverflow.com, parses it, and combines it with
// HTML markup. Returns HTML string.
private ArrayList loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
    InputStream stream = null;
    // Instantiate the parser
    StackOverFlowXmlParser stackOverflowXmlParser = new StackOverFlowXmlParser();
    List<StackOverFlowXmlParser.Entry> entries = null;
    String title = null;
    String url = null;
    String summary = null;
    Calendar rightNow = Calendar.getInstance();
    // Checks whether the user set the preference to include summary text
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    boolean pref = sharedPrefs.getBoolean("summaryPref", false);
    // List to store the items
    //ArrayList<String> feed = new ArrayList<String>();
    try {
        stream = downloadUrl(urlString);
        entries = stackOverflowXmlParser.parse(stream);
        // Makes sure that the InputStream is closed after the app is
        // finished using it.
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
    for (StackOverFlowXmlParser.Entry entry : entries) {
        feed.add(entry.title);
    }

    //return arraylist of string
    return feed;
}
// Implementation of AsyncTask used to download XML feed from stackoverflow.com.
private class DownloadXmlTask extends AsyncTask<String, Void, ArrayList> {
    @Override
    protected ArrayList doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            //return getResources().getString(R.string.connection_error);
            return null;
        } catch (XmlPullParserException e) {
            //return getResources().getString(R.string.xml_error);
            return null;
        }
    }
    @Override
    protected void onPostExecute(ArrayList result) {
        //setContentView(R.layout.activity_list_feed);
        Log.d("feed size 2", Integer.toString(feed.size()));
    }
}

}

通过放置

ListView listView = getListView();
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_list_feed,feed));

onPostExecute()方法中。

最新更新