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?