Bill L
Bill L

Reputation: 2826

setAdapter undefined inside of AsyncTask

So I am making an app in which the MainActivity is based mainly around a ListView (in fact, that's all the MainActivity is.) So I extended my MainActivity from ListActivity. The problem is, I'm trying to update my UI Thread from inside of AsyncTask and it is giving me an error that says The method setAdapter(ArrayAdapter<String>) is not defined for the type MainActivity.GetContactInfo

So I'm wondering how do I go about getting setAdapter defined. If I have to actually instantiate a ListView item, which ListView item would I instantiate? I am trying to make a custom layout for my ListView, so I am using a list_item xml file, while my activity_main.xml file has only a ListView in it, so I don't think I would grab that ListView to instantiate a ListView.

Any help on how to simple get setAdapter to work without needing to be attached to a ListView would be helpeful (since I extended my class from ListView I shouln't have to attach that to a ListView is how I see it.)

Thanks in advance.

EDIT: AsyncTask code:

    private class GetContactInfo extends AsyncTask<String, Void, ContactInfo[]> {

    @Override
    protected ContactInfo[] doInBackground(String... url) {
        URL json = null;

        try {
            json = new URL(url[0]);
        } catch (MalformedURLException e1) {
            e1.printStackTrace();
        }

        try {
            contacts = mapper.readValue(json, ContactInfo[].class);
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contacts;
    }

    @Override
    protected void onPostExecute(ContactInfo[] result) {
        int length = result.length;
        ArrayList<String> names = new ArrayList<String>();

        for (int i = 0; i < length; i++) {
            names.add(result[i].getName());
        }

        adapter = new ArrayAdapter<String>(local, R.layout.list_item, R.id.contactName, names);

        setAdapter(adapter);
    }

}

Upvotes: 0

Views: 319

Answers (1)

ucsunil
ucsunil

Reputation: 7494

setAdapter() method belongs to a ListView object. If you have extended from a ListActivity, you need to use setListAdapter(ArrayAdapter). ListActivity defines the setListAdapter() method while the ListView object defines a setAdapter method.

Change setAdapter() to setListAdapter() and that will work.

Upvotes: 1

Related Questions