Change item color in a listview that contains certain TEXT

Asked

Viewed 108 times

0

How do I put it for when a listview item has information it gets a background with a different color? In the example below it receives the data and returns them in listview:

public List<Cliente> todosOsClientes() {

    List<Cliente> users = new ArrayList<>();
    for (int i = 0; i < nome.length; i++) {
        users.add(new Cliente(" " + nome[i], " Telefone: " + telefone[i], " Celular: " + celular[i],
                " Renda: R$ " + renda[i], " Limite de Crédito: R$ " + limite[i], " Status: " + status[i],
                " Bandeira: " + bandeira[i], "" + id[i]));
    }
    return users;
}

Inside the gone I tried:

if(status[i].contains("Devendo")){
            lista.setBackgroundColor(0xFFDDEEFF);
        }

But all of it is in blue, I wanted only those that contain the text "Owing"

Basedapter:

public class AdapterClientesPersonalizado extends BaseAdapter {

private final List<Cliente> cliente;
private final clientes act;

public AdapterCursosPersonalizado(List<Cliente> cliente, clientes act) {
    this.cliente = clientes;
    this.act = act;
}

@Override
public int getCount() {
    return cliente.size();
}

@Override
public Object getItem(int position) {
    return cliente.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = act.getActivity().getLayoutInflater().inflate(R.layout.lista_cliente_personalizada, parent, false);

    Cliente cliente = cliente.get(position);

    TextView nome = (TextView)
            view.findViewById(R.id.lista_personalizada_nome);
    TextView telefone = (TextView)
            view.findViewById(R.id.lista_personalizada_telefone);
    TextView celular = (TextView)
            view.findViewById(R.id.lista_personalizada_celular);
    TextView renda = (TextView)
            view.findViewById(R.id.lista_personalizada_renda);
    TextView limite = (TextView)
            view.findViewById(R.id.lista_personalizada_limite);
    TextView status = (TextView)
            view.findViewById(R.id.lista_personalizada_status);
    TextView bandeira = (TextView)
            view.findViewById(R.id.lista_personalizada_bandeira);
    TextView id = (TextView)
            view.findViewById(R.id.lista_personalizada_id);

    nome.setText(cliente.getNome());
    telefone.setText(cliente.getTelefone());
    celular.setText(cliente.getCelular());
    renda.setText(cliente.getRenda());
    limite.setText(cliente.getLimite());
    status.setText(cliente.getStatus());
    bandeira.setText(cliente.getBandeira());
    id.setText(cliente.getId());

    return view;
}

}

  • In case list would be your listview? What is the Adapter of the data? With a custom Adapter you can do this in the item. But it would encourage you to use a Recyclerview and do the Adapter.

  • Yes, the list is listview, the Adapter is the Basedapter

  • has how you post the basedapter? so I can help better

  • Blz, just a moment

1 answer

0


I just can’t remember if parseColor will accept the way I did, I believe so.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = act.getActivity().getLayoutInflater().inflate(R.layout.lista_cliente_personalizada, parent, false);

Cliente cliente = cliente.get(position);

TextView nome = (TextView)
        view.findViewById(R.id.lista_personalizada_nome);
TextView telefone = (TextView)
        view.findViewById(R.id.lista_personalizada_telefone);
TextView celular = (TextView)
        view.findViewById(R.id.lista_personalizada_celular);
TextView renda = (TextView)
        view.findViewById(R.id.lista_personalizada_renda);
TextView limite = (TextView)
        view.findViewById(R.id.lista_personalizada_limite);
TextView status = (TextView)
        view.findViewById(R.id.lista_personalizada_status);
TextView bandeira = (TextView)
        view.findViewById(R.id.lista_personalizada_bandeira);
TextView id = (TextView)
        view.findViewById(R.id.lista_personalizada_id);

nome.setText(cliente.getNome());
telefone.setText(cliente.getTelefone());
celular.setText(cliente.getCelular());
renda.setText(cliente.getRenda());
limite.setText(cliente.getLimite());
status.setText(cliente.getStatus());
bandeira.setText(cliente.getBandeira());
id.setText(cliente.getId());

if(cliente.getStatus().contains("Devendo")){

  view.setBackgroundColor(0xFFDDEEFF);
}

return view;
}
  • The app stopped working mano kkkkkk, it worked no

  • What mistake? I’m answering by cell phone sometimes passed something unnoticed

  • I got aq, in this part:view.setBackgroundColor(Color.parseColor("0xFFFDEFF")); it should be: view.setBackgroundColor(0xFFDEFF));

  • Vlw man, mt thanks.

  • Ah, and something else here: if(client.getStatus.contains("Should")){ tem () in getStatus, it is: if(client.getStatus(). contains("Should")){

  • I didn’t need to pass the color by string, when I used it as a parameter in the json I received. I’ll edit, I’m glad it worked.

Show 1 more comment

Browser other questions tagged

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