I have simple code snippet to implement custom list view.
In this code, I have CustomAdapter
class which extends ArrayAdapter
:
CustomAdapter adapter = new CustomerAdapter(getApplicationContext(),R.layout.listview_item_row, weather_data);
The constructor of CustomAdapter
is as below:
public CustomerAdapter(Context context, int layoutResourceId, weather[] data) {
super(context, layoutResourceId, data);
mlayoutResourceId = layoutResourceId;
mcontext = context;
mdata = data;
}
If I write this, the logcat shows following error:
Java.lang.ClassCastException: android.app.Application context can not be cast to android.app.Activity
So I changed getApplicationContext()
to this
. and it worked fine:
CustomAdapter adapter = new CustomerAdapter(this,R.layout.listview_item_row, weather_data);
So, my question is : 1. why we can't pass Application context here? (for the customadapter). and
2. Which are the scenarios in which we must have to pass Activity context instead of Application context?
[Note: I have already read some answers here and here, but they do not concern with specific issue here. All are saying that 'you can use any of them', but in specific situation like this, it does not work. So, please do not mark as duplicate.]