Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamically change TextView font color in ListView

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);
    }
}
like image 701
Kris B Avatar asked Aug 21 '11 03:08

Kris B


2 Answers

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);
}
like image 159
Mikita Belahlazau Avatar answered Oct 19 '22 00:10

Mikita Belahlazau


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);
like image 24
Ronnie Avatar answered Oct 19 '22 01:10

Ronnie