I have a ListView in which every second item should have a different color (for example: white -> gray -> white ->gray etc.), it seems to work initially but as soon as I start scrolling, the rendering of the backgroundcolor seems to fail: the other color is set randomly to the items, also when I scroll up again this happens to the items where the color was correct at the beginning. Can someone hint me why this is happening?
I have tried this in the Adapter:
if((position % 2) == 1) {
layoutHolder.setBackgroundColor(getContext().getResources().getColor(R.color.light_green));
}
and this in my ListFragment:
private AsyncTask<Void, Void, Void> fillList = new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
addListItems();
return null;
}
@Override
protected void onPostExecute(Void result) {
for (int i = 1; i < getListView().getChildCount()-1; i = i+2) {
LOGI(TAG, "for-loop " + String.valueOf(i));
getListView().getChildAt(i).setBackgroundColor(getSherlockActivity().getResources().getColor(R.color.light_green));
}
}
};
UPDATE:
I have done a bit of logging and found out that, if I am adding more elements than visible on the screen and check for the ListViews child count, the ListView only holds the children which are initially visible to the screen, the rest seems to be loaded/added when scrolling is done.
in your adapter use this code.. its work.. u need to provide else condition.