Change the Background color of the ListView when the key value is found

1

In my app I have a ListView that is populated by BaseAdapter . So in BaseAdapter I have an IF statement to change the Background of the list when the value of one variable was equal to the value of another global variable.
The problem is BaseAdapter changes the color when the values of the two variables are equal (this happens only once) and also changes the color in other wrong places !!! ??? Here is the code for the getView method to see if anyone can help me:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View view = convertView;
    ViewHolder viewholder;

    if(convertView == null){

        LayoutInflater inflater = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.textview_personalizado_dialog, null);

        viewholder = new ViewHolder();

        viewholder.estacao = (TextView) view.findViewById(R.id.textView1);
        viewholder.horas = (TextView) view.findViewById(R.id.textView2);

        view.setTag(viewholder);

    } else {

        viewholder = (ViewHolder) convertView.getTag();

    }

    String estacaoHora = lista.get(position);
    String[] tokens = estacaoHora.split("/");

    String valor1 = tokens[0];// Estação
    String valor2 = tokens[1];// Hora
    String valor3 = tokens[2];// Minutos

    // Quando o valor da variavel "valor1" for igual ao valor da variavel "estacaoPesquisa" altera a cor de Background:
    if (valor1.equals(estacaoPesquisa)){

        // Altera a cor de fundo da lista:
        viewholder.estacao.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));
        viewholder.horas.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));

        viewholder.estacao.setText("  " + valor1);
        viewholder.horas.setText(valor2 + ":" + valor3);

    } else {

        viewholder.estacao.setText("  " + valor1);
        viewholder.horas.setText(valor2 + ":" + valor3);

    }

    return view;
}

// Classe interna:
static class ViewHolder {

    TextView estacao;
    TextView horas;

}
    
asked by anonymous 19.05.2015 / 01:42

1 answer

1

The problem here arises for the same reason as your other question .

Android, so you do not have to always inflate the view of each of the ListView lines, views no longer used. When there is a view that can be reused, it is passed in the convertView parameter of the getView() method. Notice that your code only does inflate when convertView is null:

if(convertView == null){

    LayoutInflater inflater = (LayoutInflater) contexto.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    view = inflater.inflate(R.layout.textview_personalizado_dialog, null);

    viewholder = new ViewHolder();

    viewholder.estacao = (TextView) view.findViewById(R.id.textView1);
    viewholder.horas = (TextView) view.findViewById(R.id.textView2);

    view.setTag(viewholder);

} else {

    viewholder = (ViewHolder) ViewHolder.getTag();
}

In addition, in order to not always have references to view elements, via findViewById , a class, ViewHolder , is used to store those references. The ViewHolder is stored in the Tag property of View .

When convertView is non-null it recovers ViewHolder by using getTag method:

.....
.....
} else {

    viewholder = (ViewHolder) ViewHolder.getTag();
}

When view is reused it comes with its set properties with previously used values. Just as you assign the values corresponding to the current line,

viewholder.estacao.setText("  " + valor1);
viewholder.horas.setText(valor2 + ":" + valor3);

You must also reset any attribute that has been changed. In this case you change the color of background :

viewholder.estacao.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));
viewholder.horas.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));

The code must be changed so that when estacaoPesquisa is not what is intended, the original background color is assigned.

if (valor1.equals(estacaoPesquisa)){

    // Altera a cor de fundo da lista:
    viewholder.estacao.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));
    viewholder.horas.setBackgroundColor(contexto.getResources().getColor(R.color.verde_5));

    viewholder.estacao.setText("  " + valor1);
    viewholder.horas.setText(valor2 + ":" + valor3);

} else {

    viewholder.estacao.setBackgroundColor(contexto.getResources().getColor(R.color.????));
    viewholder.horas.setBackgroundColor(contexto.getResources().getColor(R.color.????));

    viewholder.estacao.setText("  " + valor1);
    viewholder.horas.setText(valor2 + ":" + valor3);

}

Note: Replace R.color.???? with the "original" color

    
19.05.2015 / 12:17