I am binding an XML document to a custom adapter. All of the items in the list initially have a font color of white. One of the nodes in the XML document has an attribute that I am checking and if the attribute is set, I'd like to change the font color of that item in the ListView to a darker color. The code I have seems to work initially, but if I scroll the list up and down, the items in the ListView that should stay white, change automatically to the dark color, for some reason:
Here is my layout code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="15dip"
android:textColor="#ffffff"
android:textStyle="bold"
android:padding="5px"
/>
</LinearLayout>
and here is my custom adapter:
private class CustomAdapter extends BaseAdapter
{
private ArrayList<FilterItem> mData = new ArrayList<FilterItem>();
private LayoutInflater mInflater;
public CustomAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void addItem(FilterItem item) {
mData.add(item);
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Object getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
ViewHolder holder;
FilterItem item = (FilterItem)this.getItem(position);
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.main, null);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
TextView tvText = holder.text;
tvText.setText(item.getTitle());
if (item.Read())
{
tv.setTextColor(Color.Gray);
}
return(convertView);
}
}
ListView reuses view, as you can see in getView
you get convertView, and you only change color from white to gray. You never restore color back to white. I suggest
if (item.Read()) {
tv.setTextColor(Color.Gray);
} else {
tv.setTextColor(Color.White);
}
Move the text and color setting code to before setting the tag. Try this code
if (convertView == null)
{
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.main, null);
holder.text = (TextView)convertView.findViewById(R.id.text);
TextView tvText = holder.text;
tvText.setText(item.getTitle());
if (item.Read())
{
tv.setTextColor(Color.Gray);
}
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
TextView tvText = holder.text;
tvText.setText(item.getTitle());
if (item.Read())
{
tv.setTextColor(Color.Gray);
}
}
return(convertView);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With