Pheepster
Pheepster

Reputation: 6347

Constructor is undefined in ArrayAdapter

I am trying to populate a ListView control in an Android app. I have looked at many code samples and all seem to suggest doing it this way. However, when I try to implement this I get the following error:

The constructor ArrayAdapter(MainActivity.ReadJsonTask, int, ArrayList) is undefined

    protected void onPostExecute(String result){
        try{
            ListView lv = (ListView)findViewById(R.id.lstItems);
            JSONObject jsonObj = new JSONObject(result);

            ArrayList<String> items = new ArrayList<String>();
            Iterator<String> looper = jsonObj.keys();
            while(looper.hasNext()){
                String key = looper.next();
                items.add(jsonObj.get(key).toString());
            }               
            adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
            lv.setAdapter(adapter);

        }catch(Exception ex){
            Log.d("ReadAdscendJsonTask", ex.getLocalizedMessage());
        }
    }

What is wrong here? Thanks!

Upvotes: 1

Views: 915

Answers (1)

harism
harism

Reputation: 6073

It seems so that this piece of code is from inner class. And in such case this refers to this inner class instead of MainActivity. Changing Context parameter to MainActivity.this for ArrayAdapter should fix the error.

Upvotes: 1

Related Questions