Listview items losing color when using the scroll bar

0

I'm using baseAdapter:

@Override
public View getView(int posicao, View convertview, ViewGroup parent) {

    View view = convertview;
    ViewHolder holder = null;

    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(R.layout.itens_lista, parent, false);
        holder.itemNome = (TextView) view.findViewById(R.id.itemNome);
        holder.itemEndereco = (TextView) view
                .findViewById(R.id.itemEndereco);
        view.setTag(holder);

    } else {

        holder = (ViewHolder) view.getTag();

    }

    Telefone item = getItem(posicao);
    holder.itemNome.setText(item.getNome());
    holder.itemEndereco.setText(item.getTelefone());
    return view;
}
  

But the items get "bugged":

     
  • When I select an item it has red background color, then I scroll and the color disappears back to the original, because I'm using viewHolder and the view is "recycled or reused";
  •   
  • There are items that have red background color without being the selected item.
  •   
        
    asked by anonymous 21.10.2014 / 00:35

    3 answers

    0

    After a lot of trouble, I got the help @ Wakim and @ Cicero Moura:

    @Override
    public View getView(int posicao, View convertview, ViewGroup parent) {
    
        View view = convertview;
        ViewHolder holder = null;
    
        if (view == null) {
            holder = new ViewHolder();
            view = inflater.inflate(R.layout.itens_lista, parent, false);
            holder.itemNome = (TextView) view.findViewById(R.id.itemNome);
            holder.itemEndereco = (TextView) view
                    .findViewById(R.id.itemEndereco);
            view.setTag(holder);
    
        } else {
    
            holder = (ViewHolder) view.getTag();
        }
    
        if (listener.getPosicao() == posicao
                && listener.statusSelecao()) {
            holder.itemNome.setBackgroundColor(Color.RED);
            holder.itemEndereco.setBackgroundColor(Color.RED);
        }
            else {
                holder.itemNome.setBackgroundColor(Color.rgb(65, 105, 225));
                holder.itemEndereco.setBackgroundColor(Color.rgb(65, 105, 225));
        }
    
        Telefone item = getItem(posicao);
        holder.itemNome.setText(item.getNome());
        holder.itemEndereco.setText(item.getTelefone());
        return view;
    }
    
        
    21.10.2014 / 15:46
    1

    Friend, it's the following:

    Every time you roll a ListView, items that "pop up" or up are destroyed.

    Then, when they are recreated again, they return to their original state without their customization.

    What you can do is create an adapter and set this state, as in the example below:

    public class MySimpleArrayAdapter extends ArrayAdapter<Item> {
      private final Context context;
      private final Item[] values;
    
      public MySimpleArrayAdapter(Context context, Item[] values) {
        super(context, R.layout.your_row_layout, values);
        this.context = context;
        this.values = values;
      }
    
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context  .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.your_row_layout, parent, false);
    
         TextView itemNome= (TextView) rowView.findViewById(R.id.itemNome);
         TextView itemEndereco= (TextView) rowView.findViewById(R.id.itemEndereco);
         CheckBox check = (CheckBox)rowView.findViewById(R.id.check);
    
    
         Item item = getItem(position);
    
         itemNome.setText(item.getNome());
         itemEndereco.setText(item.getEndereco());
    
         if(check.isChecked()){
    
          rowView.setBackgroundColor(Color.RED);
    }
    else{
    
         rowView.setBackgroundColor(Color.WHITE);
    
    }
    
        return rowView;
      }
    } 
    

    The code above is just an example.

    But I hope what you have really understood is that items are destroyed when scrolling and rebuilt when viewed, returning to the original state.

    On the error that occurs when scrolling farther down, in the adapter, you can put a breackpoint and debug.

    I saw that you are using the ViewHolder pattern, so just look at that fact, from recycling.

        
    21.10.2014 / 04:57
    0

    Try using setCacheColorHint

    Ex:

    mDrawerList = (ListView) findViewById(R.id.hinario_left_drawer);
    mDrawerList.setCacheColorHint(Color.parseColor("#669966"));
    
        
    24.10.2014 / 05:14