justauser
justauser

Reputation: 37

The constructor ArrayAdapter<String>(int, ArrayList<String>) is undefined

Well the title says it all. I'm not sure why it doesn't want to work.

This is in an AsyncWorker so that might be the issue. Also the variables are outside of a protected method but the same happens when inside.

private class MyAsyncTask extends AsyncTask<String, String, String> {


    ListView workersList = (ListView)findViewById (R.id.workers_list);
    ArrayList<String> itemsList = new ArrayList<String>();


    protected String doInBackground(String... arg0) {

Then later on

ArrayAdapter<String> adapter = new ArrayAdapter<String>(android.R.layout.simple_list_item_1, itemsList);

Upvotes: 2

Views: 8251

Answers (3)

mike20132013
mike20132013

Reputation: 5425

You need to add a context to match the parameters required.

Change your code: ArrayAdapter adapter = new ArrayAdapter(android.R.layout.simple_list_item_1, itemsList);

To

Define your context,

Context context; ArrayAdapter mArrayAdapter; . . . And when setting your adapter, mArrayAdapter = new ArrayAdapter(context,android.R.layout.simple_list_item_1, itemsList); mListView.setAdapter(mArrayAdapter);

Hope this helps.. :)

Upvotes: 0

Tyler Ferraro
Tyler Ferraro

Reputation: 3772

Check out the documentation on ArrayAdapter. It doesn't have a constructor with parameters (int, ArrayList). If you prepend context as well then you'll have a constructor that exists.

ArrayAdapter<String> adapter = new ArrayAdapter<String>(<ActivityName>.this, android.R.layout.simple_list_item_1, itemsList);

More info: http://developer.android.com/reference/android/widget/ArrayAdapter.html

Upvotes: 2

Amulya Khare
Amulya Khare

Reputation: 7698

You forgot the context parameter. Change to:

ArrayAdapter<String> adapter = new ArrayAdapter<String>(<ActivityName>.this, android.R.layout.simple_list_item_1, itemsList);

where replace <ActivityName> with the name of your activity class.

Upvotes: 7

Related Questions