Change List View item color if it is disabled - Android

0

I have a normal listview that lists the names of my users.

And I would like the disabled users to have the gray bottom of the list item. How to do this?

public class Usuario {

private String nome;
private String endereco;
private Boolean ativo;


//construtor
// getters e setters

}

That is, if active is true, leave normal. If it is false, leave it gray.

How do I change the color of the List View item if it is disabled?

    
asked by anonymous 04.07.2015 / 01:42

1 answer

1

To do this properly, you need to create a Adapter that will receive your user, make the check active and inactive, and create a ListView item for it.

You will need:

  • A class ( e.g. UsuarioListAdapter ) that extends BaseAdapter ;
  • A layout ( e.g. adapter_item_usuario ), which UsuarioListAdapter will use to put the data of its Usuario class.
  • Get to work!

    • Create the layout for the list items:

      <?xml version="1.0" encoding="utf-8"?>
      
      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >
      
      <TextView android:id="@+id/textview_nome"/>
      
      <TextView android:id="@+id/textview_endereco"/>
      
      </RelativeLayout>
      
    • Now create the class that extends BaseAdapter :

      public class UsuarioListAdapter extends BaseAdapter {
      
          Context mContext;
          List<Usuario> mUsuarios = new ArrayList<Usuario>();
      
          public UsuarioListAdapter(Context context, List<Usuario> usuarios){
      
              mContext = context;
              mUsuarios = usuarios;
          }
      
          @Override
          public int getCount() {
              return mUsuarios.size();
          }
      
          @Override
          public Object getItem(int position) {
              return mUsuarios.get(position);
          }
      
          @Override
          public long getItemId(int position) {
              return position;
          }
      
          // Neste método que é feita a verificação e a mudança de cor do item
          @Override
          public View getView(int position, View view, ViewGroup parent){
      
          LayoutInflater layoutInflater = (LayoutInflater) context
          .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      
          view = layoutInflater.inflate(R.layout.adapter_item_usuario,null);
      
          // Verificação e mudança de cor
          if(mUsuario.get(position).estaAtivo == false){
      
              view.setBackgroundResource(R.color.Grey);
      
          }
      
          TextView mTextviewNome = (TextView) view.findViewById(R.id.textview_noticia);
          TextView mTextViewEnderco = (TextView) view.findViewById(R.id.textview_noticia_data);
      
          mTextviewNome.setText(mUsuarios.get(position).getNome());
          mTextViewEnderco.setText(mUsuarios.get(position).getEndereco());
      
          return view;
      }
      

    Now, instead of creating a SimpleArrayAdapter , create a UsuarioListAdapter by passing the correct parameters and setting it as the adapter of your ListView .

        
    04.07.2015 / 17:36