2

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);
1
  • You need to add a context to the constructor. This answer is extremely easily found if you just looked at the documentation. Commented Dec 31, 2013 at 4:12

3 Answers 3

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok so if my activity is named MainActivity it would be ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, itemsList);?
2

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

Comments

0

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.. :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.