2

I'm trying to add strings into a ListView, which gets called inside another ListView (changing the ContentView inside the OnItemClickListener )

Here's the code:

 lv1.setOnItemClickListener(new OnItemClickListener(){
    public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
            long arg3) {
        // TODO Auto-generated method stub

        if(lv1.getItemAtPosition(pos).equals(cat[0]))                                                       
        {                                                                                                           
            setContentView(R.layout.browse_engineering);
            final ListView engList = (ListView)findViewById(R.id.eng_list);
            engList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,eng_build));


        }                                                                                                                                                                                                     
    }
 });

And this is the error I get:

Error image.

1
  • 3
    The ArrayAdapter constructor wants a Context as the first argument; Activities are Contexts. Since you're instantiating it inside an OnItemClickListener, the this keyword does not refer to the Activity. That needs to be fixed. Commented Dec 25, 2011 at 5:18

1 Answer 1

3

Replace this

engList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,eng_build));

By this,

engList.setAdapter(new ArrayAdapter<String>(yourActivity.this,android.R.layout.simple_list_item_1,eng_build));

As onItemClickListners are not contexts.

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

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.