This post will be heavy on images.
I have created a custom ArrayAdapter and using it on a Listview. Am I missing something here on the Adapter?
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
ArrayHolder holder = null;
if(row == null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ArrayHolder();
holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
row.setTag(holder);
}
else
{
if(position % 2 == 0){row.setBackgroundColor(Color.DKGRAY);}
else{row.setBackgroundColor(Color.BLACK);}
Typeface font1 =Typeface.createFromAsset(context.getAssets(),"fonts/kingrich.ttf");
((TextView)row.findViewById(R.id.txtTitle)).setTypeface(font1);
((TextView)row.findViewById(R.id.txtTitle)).setTextColor(Color.rgb(153, 255, 102));
holder = (ArrayHolder)row.getTag();
}
String array = data.get(position);
holder.txtTitle.setText(array);
return row;
}
When I first launch the application, the following screen is displayed:
(Screens are form an actual device and not an emulator)
Now if I close the virtual keyboard with the back button, on the bottom I can already see one row now being colored incorrectly, when I scroll down a little:
Scroll down a little to see the “bad” row
If I scroll up and down, the row will get the coloring correct.
Thanks in advance
It looks to me that you are not handling correctly the creation of new views.
The text appears as white, this tells us that the else block is skipped (so the convertView parameter was null, or that there are no views to recycle (which is fair, since you see many more views when the softkeyboard disappears)
The row gets colored correctly after a scroll up/down because you now have views to recycle and therefore you will enter the else block.
Imho you should remove altogether the else keyword.
This way you will enter the if (where you create the new view) only if there is actually the need, but otherwise you will always put the correct color even if the view is new and not recycled.
Give it a try and tell me if it works