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",
};
}
Your TextView does nothing. Typically you need to use
findViewById()to locate the TextView in your row’s layout. Since you are usingandroid.R.layout.simple_list_item_1which is a TextView itself, we can skip that step: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.I assume that this change happens when you scroll the ListView, this is directly linked to how the View recycler behaves.