user3585510
user3585510

Reputation: 151

Passing List<classname> to ArrayAdapter

The problem is this adapter is giving the error although i have pass the Object array to it.(Read the methods belows then you will find what i want to know from you guys)

This method declares a List of private class objects. Then return that list of object to onPostExecute method.

private class DownloadXmlTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {
        try {

            return loadXmlFromNetwork(urls[0]);

        } catch (IOException e) {
            return "I/O exception ae hy";
        } catch (XmlPullParserException e) {
            return "XML pull parser ke exception ae hy";
        }
    }

    @Override
    protected void onPostExecute(List<StackOverflowXmlParser.Entry> result) {
        //Log.d(TAG,result.toString());
        ArrayAdapter<String> adapter;
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,result);
        setListAdapter(adapter);
    }

    private Object 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;


        try {
            stream = downloadUrl(urlString);
            entries = stackOverflowXmlParser.parse(stream);

        } finally {
            if (stream != null) {
                stream.close();
            }
        }
        for (StackOverflowXmlParser.Entry entry : entries)
        {
            Log.d(TAG, entry.link + " /" + entry.title);
        }

            return entries;
    }

Upvotes: 1

Views: 1343

Answers (2)

Alexander
Alexander

Reputation: 48232

I think it should be onPostExecute(List<StackOverflowXmlParser.Entry> result)

And you AsyncTask should be

extends AsyncTask<smth, smth, List<StackOverflowXmlParser.Entry> >

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006604

ArrayAdapter<String> requires that you provide it a String[] or a List<String>. You are trying to pass in Object[], which is neither String[] nor List<String>. And, it would appear that you are really trying to populate the ListView with a list of StackOverflowXmlParser.Entry objects, which are not String objects.

My guess is that the right answer is for you to create an ArrayAdapter<StackOverflowXmlParser.Entry> instead of an ArrayAdapter<String>.

Regardless, you need to ensure that the data type in your declaration (String in ArrayAdapter<String>) matches the data type in your constructor parameter that supplies the data to be adapted.

Upvotes: 0

Related Questions