Change the Listview Background color when the key value is found

Asked

Viewed 890 times

2

In my app I have a ListView which is provided through BaseAdapter.
Then in the BaseAdapter i have an IF statement to change the list background when the value of a variable was equal to the value of another global variable.
The problem is the BaseAdapter changes the color when the values of the two variables are equal (this only happens once) and also changes the color in other wrong places!!!??? Here’s the method code getView 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;

}
  • Look at this reply maybe it’ll help.

  • Hello ramaral! The post you indicated is mine. Here my request for help is different, that is, I do not need to make a zebra list, I want to make a normal list but when the String value of a given list is equal to the value of a variable I want to change the Background color of that list (which will be unique at Listview)...

  • I had not noticed that the post was yours. The problem here arises for the same reason. I see you did not understand the problem. I will post an answer.

  • Thank you ramaral. I’m waiting...

1 answer

2


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

Android, so it is not necessary to always be doing the inflate view of each of the Listview, has a mechanism that allows reuse views unused.
When there is a view that can be reused it is passed in the parameter convertView of the method getView(). Notice that its code only does the inflate when the convertView is void:

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

Additionally, so you don’t always have to get references to the elements of the view, through findViewById, a class is used, a ViewHolder, to store these references. A ViewHolder is stored on the property Tag of View.

When the convertView it is not null recover the ViewHolder using the method getTag:

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

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

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

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

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

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

The code will need to be changed so that when the estacaoPesquisa is not intended, the "original" colour of the 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.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.???? by the color "original"

  • That was the problem!! Indeed I had not intended well the logic of the operation of the getView method. It was excellent his explanation. Thank you very much. Hug.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.