-2

I am Trying to inflate one ListView When clicking a member of another ListView Which is in a ListActivity. So when I try to set Adapter it asks me for getApplicationContext, so the ListView is in another activity and I am trying to call that that applicationContext from another the ListActivity which will inflate the ListView if you click it.

I am Doing this and I dont Know If I am right or wrong

public class FirstList extends ActionBarActivity{
    public static ListView firstL;
    public static ArrayList<Friend> friendSelected= new ArrayList<>();
    public static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.conversation_list_layout);

        context = getApplicationContext();
        firstL= (ListView)findViewById(R.id.listView1);
   }
}

Second Class

   private class FriendSelectorAdapter extends ArrayAdapter<Friend> {
        public ChatListAdapter(){
            super(FirstList.context,R.layout.list_layout, FirstList.friendSelected);
        }

In the second class there is a ListActivity which is displaying all my contacts and this adapter is meant to be called when one of the contacts is clicked so it will populate the listView of the first class.

Am I wrong what i am doing here? I would appreciate any answer, Thanks

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
kobbycoder
  • 682
  • 2
  • 10
  • 33
  • 1
    **Do NOT use application context to inflate views** (which also means you can't pass it to adapters). If your activity had a different theme than your application you would notice. Also with an app context your app will crash when launching links from TextViews. **When working with views always use the activity context** or provided layout inflaters (in fragments). – Eugen Pechanec Apr 13 '15 at 11:50
  • though it is not crashing – kobbycoder Apr 13 '15 at 22:50
  • 1
    Did you read the whole comment? In your case it's not crashing does not mean you're using it improperly. – Eugen Pechanec Apr 14 '15 at 02:01

1 Answers1

0

Change constructor to some think like this(without any static in activity):

public ChatListAdapter(Context context, List<Friend> friendSelected){ super(context,R.layout.list_layout,friendSelected); }

Piotr Golinski
  • 990
  • 8
  • 19