3

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

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?

Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
Aaloka
  • 115
  • 2
  • 11
  • 1
    use android:cacheColorHint="#00000000" in ur urlistview.xml file – Sam May 17 '13 at 05:07
  • what is `GridviewAdapter.glu` ? where r u storing the marks?r the marks displayed in any textview? it will be easy to help if u can post whole class or if u describe what u r doing. – Mehul Joisar May 17 '13 at 05:09

4 Answers4

7

I think it is because list view recycles the view hence causing such problems .Try the following :

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        convertView = null;
        row = convertView;

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) _context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.row, parent,
                    false);
                       // your code 


          }
     return row;
}

Since convert view and row view both are intialized as null .Hence rows will be inflated every time and preventing recycling of views .

Link: Listview android recycling This links explains the mechanism of recycling views.

  • use the above code if wrong views are changing color.
  • if you are trying to say on touching views while scrolling background becomes black then use the following in list view

android:cacheColorHint="#00000000"

Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
  • Hey thank you but im having a problem at this line row = inflater.inflate(R.layout.row, parent,false); – Aaloka May 17 '13 at 05:14
  • its "rachita " not "richita "!! It will be more helpful if you post your entire logcat error message ,so that everyone can see the exact error. – Rachita Nanda May 17 '13 at 05:24
  • http://stackoverflow.com/questions/5143369/main-cannot-be-resolved-or-is-not-a-field .Check this ,it may solve your error – Rachita Nanda May 17 '13 at 05:30
  • @Rachita you should not set the convertview to null – vinoth May 17 '13 at 05:41
  • Y so ? please let me know will it cause any problems ?? I fixed by problem by doing this . – Rachita Nanda May 17 '13 at 05:43
  • I think it may not give any error but it may affect the performance and also spoils the cause of having a view holder class.because we are doing so in order to avoid the costly findViewById(). more is in this [Listview Perfomance tips](http://lucasr.org/2012/04/05/performance-tips-for-androids-listview/) – vinoth May 17 '13 at 05:48
  • ok thanks :) But I was unable to find any better solution to this problem! Pls let me know if you find one – Rachita Nanda May 17 '13 at 05:52
  • Thanks for pointing out the ListView recycling mechanism. Knowing it really solved my ListView adapter problem. – lcn Dec 31 '13 at 22:28
6

Try 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());


//**********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;
    }
Arun C
  • 9,035
  • 2
  • 28
  • 42
0

You have to do something like this,

if(Yourmark[position]<50) //Where Yourmark[] is the mark array you are passing to your custom adapter
{
        holder.img.setImageResource(R.drawable.red_arrow);
        holder.txtViewDescription.setBackgroundResource(R.color.resultred);  
        holder.txtholder.setBackgroundResource(R.color.resultred);  
}

else
{
        holder.img.setImageResource(R.drawable.green_arrow);
        holder.txtViewDescription.setBackgroundResource(R.color.resultgreen);  
        holder.txtholder.setBackgroundResource(R.color.resultgreen);  
}
}
Basim Sherif
  • 5,384
  • 7
  • 48
  • 90
0

Set this code on adapter (custom), so you can recycle the view with the previous color only if the view is selected.

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE );

    Task task = taskArrayList.get(position);

    View view = inflater.inflate(R.layout.task_row, parent, false);

    if(MyActivity.getIsClicked() && MyActivity.getPositionClicked() == position){
        view.setBackgroundResource(R.color.backgroundSelectedItem);
    }
noone000
  • 147
  • 1
  • 10