Android Listview text colour white onCreateView(instead of black)
Asked Answered
F

3

2

Attached are 2 photos one when the list is populated using the onCreateView when the app starts(white text) and the other is whenever the SetList() method is called

https://i.sstatic.net/QHNEL.png

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ListView lv = (ListView) rootView.findViewById(R.id.listView1);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);
        lv.setAdapter(adapter);
        return rootView;
    }

https://i.sstatic.net/8uTe1.png

    public void SetList()
{
    ListView lv = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Logs);
    lv.setAdapter(adapter);
}
Finite answered 13/5, 2014 at 12:36 Comment(0)
F
8

Problem solved by changing ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);

to

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getBaseContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);

it seems like for some reason ((MainActivity)getActivity()).getApplicationContext() returns a different default text color than ((MainActivity)getActivity()).getBaseContext()

Finite answered 14/5, 2014 at 19:23 Comment(0)
M
0

Change

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_selectable_list_item,((MainActivity)getActivity()).Logs);

to

ArrayAdapter<String> adapter = new ArrayAdapter<String>(((MainActivity)getActivity()).getApplicationContext(), android.R.layout.simple_list_item_1,((MainActivity)getActivity()).Logs);

in onCreateView. The problem is that you are using 2 different layouts in the adapter. Using android.R.layout.simple_list_item_1 should give you TextView with black text the first time too.

Maquette answered 13/5, 2014 at 12:39 Comment(3)
It was already that I seemed to have copied a version that I was trying fixes on OP edited to reflect thisFinite
Did you try to run after setting both adapter to android.R.layout.simple_list_item_1?Maquette
I did same issue occurs.Finite
B
0

try this, Override getView method and set text color like this

 countryadapter = new ArrayAdapter<String>(CreateGroupsActivity.this,
            android.R.layout.simple_spinner_item,
            CommonListClass.countryName) {
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getView(position, convertView, parent);
            ((TextView) v).setTextColor(getResources().getColor(R.color.black_color));
            return v;
        }

        @Override
        public View getDropDownView(int position, View convertView,
                ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = super.getDropDownView(position, convertView, parent);
            ((TextView) v).setTextColor(getResources().getColor(R.color.black_color));
            return v;
        }
    };
Buffington answered 13/5, 2014 at 12:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.