John Jared
John Jared

Reputation: 800

Android ListView Text Color white

I have a custom listview by using an adapter which has alternate background color. The problem is that the text which are strings (over 100+ of strings) are set to the color of Color.BLACK but in the listview the first couple of strings are white and then the rest are black.

@Override  
    public View getView(int position, View convertView, ViewGroup parent) {  
    View view = super.getView(position, convertView, parent);  
    TextView tv = (TextView) super.getView(position, convertView, parent);
    int colorPos = position % color.length; 
    tv.setTextColor(Color.BLACK);
    view.setBackgroundColor(color[colorPos]);  
    return view;  
}  

I can't detect the problem why the first are white then the others are black, is it something to with loading or something? because my text (the strings) they are over 100.

UPDATE

This is the listView code:

MyAdapter adapter = new MyAdapter(this, 
    android.R.layout.simple_list_item_1,
    Strings.Advices);
setListAdapter(adapter);

And the strings:

public class Strings {
    public static String Advices[] = {
       "advice",
       "advice",
       "adc",
       "add",
    };
}  

Upvotes: 2

Views: 5234

Answers (1)

Sam
Sam

Reputation: 86948

Your TextView does nothing. Typically you need to use findViewById() to locate the TextView in your row's layout. Since you are using android.R.layout.simple_list_item_1 which is a TextView itself, we can skip that step:

@Override  
public View getView(int position, View convertView, ViewGroup parent) {  
    View view = super.getView(position, convertView, parent);  
    TextView tv = (TextView) view;
    int colorPos = position % color.length; 
    tv.setTextColor(Color.BLACK);
    view.setBackgroundColor(color[colorPos]);  
    return view;  
}  

But the getView() method of any built-in Adapter is very generic, therefor slow... You should write your own to take full advantage of the Adapter's recycler and the ViewHolder concept. Please watch this Google Talk Turbo Charge Your UI (and/or the World of ListView) to help you write an efficient adapter.

The problem is that the text which are strings ( over 100+ of strings ) are set to the color of Color.BLACK but in the listview the first couple of strings are white and then the rest are black

I assume that this change happens when you scroll the ListView, this is directly linked to how the View recycler behaves.

Upvotes: 3

Related Questions