简体   繁体   中英

Change the text color of one row of a ListView

I have a ListView which is correctly filled and it displays all its items. I was wondering how could I change the color of one of its rows (index = 0). I was trying to use the ListView and reaching its child using listView.getChildAt(0) but I do not see any method to change the color of the text (just for the background)

Just in case is needed I post the method that I use to update the ListView

String[] aux = new String[namesList.size()];
            namesList.toArray(aux);
            arrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.row, aux);
            listView.setAdapter(arrayAdapter);
            if(from)
                listView_address.getChildAt(0) // Not a method which I could use
            setSelectNameListeners();

You cannot change the color of a String, however you can change color of a textview. A String only tells what to display, a textView will be responsible for how to display the information, now to change the color of the text you'll need a custom adapter and make that adapter return you the items, you get the 1st item and change it's color.

Yo can try this:

        String[] aux = new String[namesList.size()];
        namesList.toArray(aux);
        arrayAdapter = new ArrayAdapter<>(getActivity(), R.layout.row, aux){
             @Override
            public View getView(int position, View convertView, ViewGroup parent){
              if(position == 0){   
               // Get the Item from ListView
               View view = super.getView(position, convertView, parent);

               // Initialize a TextView for ListView each Item
               TextView tv = (TextView) view.findViewById(android.R.id.text1);

               // Set the text color of TextView (ListView Item)
               tv.setTextColor(Color.RED);

               // Generate ListView Item using TextView
               return view;
               }   
             }
        };
        listView.setAdapter(arrayAdapter);
        

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM