How to update the background colors of the rows of a listView?

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 arrayAdapter based on this information. How do I call the arrayAdapter from my activity. When I use tableLayout I remove all the view from the table and insert again, this resolves my problem, but in the listView if I do this error. Does anyone know a good way to update the listView without having to kill the activity and create it again?

    
asked by anonymous 14.07.2016 / 20:40

2 answers

1

With the adapter you can call the method:

adapter.notifyDataSetChanged();

Once this is done, it updates the listView

    
14.07.2016 / 21:50
1

Daniel,

To implement this color change in the ListView, make a customAdapter that extends the ArrayAdapter, then 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 put a sample class just for you to understand.

Using this adapter, in your Activity call

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

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

    
15.07.2016 / 15:08