滚动时Android系统的列表视图项目的背景颜色变化

问题描述:

我的的ListView 包含两个 Textviews 。在一行中第一个是对名,第二个为结果。我需要改变根据结果结果的TextView 的背景颜色。就像如果通过,那么结果失败时的结果的TextView 颜色为绿色的TextView 颜色会red.I有十行在我的的ListView 。我试图像这样

My ListView contains two Textviews. In one row first one is for name and second one is for result. I need to change the background color of the result TextView according to the result. Like if pass then the result TextView color will be green when fails result TextView color will be red.I have ten rows in my ListView. I have tried like this

    @Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    ViewHolder holder;
    if(convertView==null)
    {
        holder = new ViewHolder();
        convertView = inflater.inflate(R.layout.items, null);

        holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
        holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
        holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
        holder.txtholder = (TextView) convertView.findViewById(R.id.holder1);
        holder.img = (ImageView)  convertView.findViewById(R.id.temperrr);
        convertView.setTag(holder);
    }
    else
        holder=(ViewHolder)convertView.getTag();

    ItemBean bean = (ItemBean) itemList.get(position);

    holder.imgViewLogo.setImageResource(bean.getImage());
    holder.txtViewTitle.setText(bean.getTitle());
    holder.txtViewDescription.setText(bean.getDescription());
  //------------------  
    if (position==0)
    {
        if(GridviewAdapter.glu>=81)
        {
        holder.img.setImageResource(R.drawable.red_arrow);
        holder.txtViewDescription.setBackgroundResource(R.color.resultred);  
        holder.txtholder.setBackgroundResource(R.color.resultred);  
        }
        else if (GridviewAdapter.glu==79||GridviewAdapter.glu==80)
        {
            holder.img.setImageResource(R.drawable.orange_arrow);
            holder.txtViewDescription.setBackgroundResource(R.color.resultoren);  
            holder.txtholder.setBackgroundResource(R.color.resultoren);  
        }
        else
        {
            holder.img.setImageResource(R.drawable.resultarrawnocolor);
            holder.txtViewDescription.setBackgroundResource(0);  
            holder.txtholder.setBackgroundResource(0);  
        }

    }
        holder.img.setImageResource(R.drawable.resultarrawnocolor);
       return convertView;
}

不过,滚动列表中的另一个随机项目的背景颜色changing.what时,我必须这样做?如何控制在ListView中每个位置的项目。以上code是只为第一行。我对吗?

But, when scrolling the list the another random items's background color changing.what I have to do .? How can I control the items at each position in the listview . The above code is for just for the first row. am I right?

试试这个

     @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        ViewHolder holder;
        if(convertView==null)
        {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.items, null);

            holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo);
            holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle);
            holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription);
            holder.txtholder = (TextView) convertView.findViewById(R.id.holder1);
            holder.img = (ImageView)  convertView.findViewById(R.id.temperrr);
            convertView.setTag(holder);
        }
        else
            holder=(ViewHolder)convertView.getTag();




        ItemBean bean = (ItemBean) itemList.get(position);

        holder.imgViewLogo.setImageResource(bean.getImage());
        holder.txtViewTitle.setText(bean.getTitle());
        holder.txtViewDescription.setText(bean.getDescription());


//**********SET ALSO YOUR DEFAULT BACKGROUND HERE******** like//

     holder.txtViewDescription.setBackgroundResource(R.color.DEFAULCOLOR);  
     holder.txtholder.setBackgroundResource(R.color.DEFAULCOLOR); 
     holder.img.setImageResource(R.drawable.defalut_image);


//**********SET ALSO YOUR DEFAULT BACKGROUND HERE******** ok//


      //------------------  
        if (position==0)
        {
            if(GridviewAdapter.glu>=81)
            {
            holder.img.setImageResource(R.drawable.red_arrow);
            holder.txtViewDescription.setBackgroundResource(R.color.resultred);  
            holder.txtholder.setBackgroundResource(R.color.resultred);  
            }
            else if (GridviewAdapter.glu==79||GridviewAdapter.glu==80)
            {
                holder.img.setImageResource(R.drawable.orange_arrow);
                holder.txtViewDescription.setBackgroundResource(R.color.resultoren);  
                holder.txtholder.setBackgroundResource(R.color.resultoren);  
            }
            else
            {
                holder.img.setImageResource(R.drawable.resultarrawnocolor);
                holder.txtViewDescription.setBackgroundResource(0);  
                holder.txtholder.setBackgroundResource(0);  
            }

        }
            holder.img.setImageResource(R.drawable.resultarrawnocolor);
           return convertView;
    }