Quantcast
Channel: dynamically change TextView font color in ListView - Stack Overflow
Viewing all articles
Browse latest Browse all 3

dynamically change TextView font color in ListView

0
0

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);
    }
}

Viewing all articles
Browse latest Browse all 3

Latest Images

Trending Articles





Latest Images