I'm using the code below for populating my list view:
String[] values = new String[] {
"Sirwan Afifi",
"Shaho Toofani",
"Hamed Ghaderi",
"Sattar Menbari"
};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, values);
setListAdapter(adapter);
layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
It works fine, But result is a list with white text color, after debugging I figured it out, problem was the first parameter of ArrayAdapter
, I just change it to this
then background changed to black!
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
my problem solved with above solution, but I'm curious to find out, what is difference between this
and getApplicationContext
in this scenario?
I also have searched and found these answers but those aren't enough to clarify my question :
Difference between getContext() , getApplicationContext() , getBaseContext() and "this"
What's the difference between the various methods to get a Context?
if the control or variable you are creating should belong to application level then use applicationContext. if the control or variable you create belong to Activity level then use "this" pointer. if this is not available, still we can get activity context by specifying ActivityName.this .