Question

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

Was it helpful?

Solution

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);

OTHER TIPS

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>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top