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