setAdapter
is an asynchronous call. After you call it, it will take some time to execute, that's why after assigning it you are getting values consistent with the "no data" state.
You need to modify your Adapter getView , that is the function called by the ListView
everytime it needs a view. You need to read about it, it's the key to understand how Android widgets really work.
In your example, this will do it:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, listItems) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=super.getView(position, convertView, parent); // we get the original view
if (position==0) view.setBackgroundColor(Color.GREEN); // and change color if position==0
return view;
}
};
... but again I recommend you to read carefully how this stuff works, find out a good tutorial on Adapters!