2

Problem with AsyncTask. It does not like my ArrayAdapter:

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, result);

Fault message: "Cannot resolve constructor"...to long to write.

The code snippet:

        private class UpdateListviewWithStringAsync extends AsyncTask<String, Void, ArrayList> {
            @Override
            protected ArrayList doInBackground(String... infos) {
                oneLogger = new ArrayList<String>();
                for (String info : infos) {
                    Log.d("Haze", info);
                    oneLogger = new ArrayList<String>();
                    oneLogger.add(info);
                }
                return oneLogger;
            }

            @Override
            protected void onPostExecute(ArrayList... result) {
                listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, result);
                listView.setAdapter(listAdapter);
            }
        }

I have had this ArrayAdapter declaration before in other parts of the code but not in this AsyncTask code. I'm guessing that I'm trying to do this:

ArrayAdapter(Context context, int resource, List objects)

Please help me understand why this is wrong:)

2 Answers 2

4

Replace "this" with ActivityName.this.

Change:

listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, result);

to:

listAdapter = new ArrayAdapter<String>(ActivityName.this, R.layout.simplerow, result);
Sign up to request clarification or add additional context in comments.

Comments

2
ArrayList... result

I'm not sure if your code will compile but from your snippet this is ArrayList[] but written as varargs. you need to use result[0] but i think that parameter of onPostExecute() method should be only ArrayList result and not array.

So, it should looks like:

@Override
protected void onPostExecute(ArrayList result) {
   listAdapter = new ArrayAdapter<String>(<context>, R.layout.simplerow, result);
   listView.setAdapter(listAdapter);
}

Context can be passed into class through constructor (this is efficient and clean solution).

Note: I recommend to use generics like this: ArrayList<String>

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.