How to update the background colors of the lines of a listview?

Asked

Viewed 61 times

2

I have a listview that when I click on a line, it sends information to the server that returns a status. I needed to change the color of the line in my Adapter based on this information. How do I call the Activity arrayAdapter. When I use the tableLayout I remove all view from the table and enter again, this solves my problem, but in listview if I do this error. Does anyone know a good way to update listview without having to kill Activity and create again?

  • Your Adapter already has the code that changes the line color ? Your question is to update the list ?

  • Try the notifyDataSetChanged(); You can call it inside the Adapter itself or in your Activity by doing: Adapter.notifyDataSetChanged();

2 answers

1

Daniel,

To implement this color change in Listview, make a customAdapter that extends the Arrayadapter, so inside it you implement this color change.

public class ArrayTeste extends ArrayAdapter {

List<Objeto> lista;
Context contexto;

public ArrayTeste(Context context, int resource, List<Objeto> lista) {
    super(context, resource, lista);
    this.lista = lista;
    this.contexto = context;

}

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

    Objeto objeto = lista.get(position);


    if(objeto.isStatus()){
       int cor = Color.parseColor("blue");
        convertView.setBackgroundColor(cor);
    }

    return super.getView(position, convertView, parent);
}

public class Objeto{
    private String Nome;

    public String getNome() {
        return Nome;
    }

    public void setNome(String nome) {
        Nome = nome;
    }

    public boolean isStatus() {
        return Status;
    }

    public void setStatus(boolean status) {
        Status = status;
    }

    private boolean Status;




}



}

In the end I set an example class just so you understand.

Using this Adapter, in your Activity call

    meuAdapter.clear();
    meuAdapter.addAll(listaObjetosAtualizados);

The command notifyDataSetChanged() will only update an item that has been added or removed, it will not update a View that is already in display.

1

With Adapter you can call the method:

adapter.notifyDataSetChanged();

Done this it updates the listview

Browser other questions tagged

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