Change the color of a list according to a certain variable

Asked

Viewed 49 times

0

I’m trying to get my schedule list to have each line with different colors for each category that was defined, this list is filled by data recovered from firebase that goes to the model class, wanted that when this Dialogfragment was called to list the schedule he differentiated each category by color (spiritual blue, yellow gymkhana, green leisure), follows the code of my Adapter where I try to change the color of each of the lines:

public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
    View view = null;

    //Validando e criando a lista
    if (arrayList != null){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);

        //Montando a view a partir do XML
        view = inflater.inflate(R.layout.lista_cronograma, parent, false);

        //Recuperando os elementos para exibição
        TextView inicio = view.findViewById(R.id.tv_hora_inicio);
        TextView fim = view.findViewById(R.id.tv_hora_fim);
        TextView atividade = view.findViewById(R.id.tv_atividade);

        Cronograma crono =  arrayList.get(position);
        inicio.setText(crono.getHoraInicio());
        fim.setText(crono.getHoraFim());
        atividade.setText(crono.getAtividade());

        switch (crono.getCategoria()){
            case "Espiritual":
                inicio.setBackgroundColor(0xCFD8DC);
                fim.setBackgroundColor(0xCFD8DC);
                atividade.setBackgroundColor(0xCFD8DC);
                view.setBackgroundColor(0xCFD8DC);
                break;
            case "Gincana":
                inicio.setBackgroundColor(0xFFF9C4);
                fim.setBackgroundColor(0xFFF9C4);
                atividade.setBackgroundColor(0xFFF9C4);
                view.setBackgroundColor(0xFFF9C4);
                break;
            case "Lazer":
                inicio.setBackgroundColor(0xDCEDC8);
                fim.setBackgroundColor(0xDCEDC8);
                atividade.setBackgroundColor(0xDCEDC8);
                view.setBackgroundColor(0xDCEDC8);
                break;
        }

    }

    return view;
}
  • What’s wrong with the code?

2 answers

0

Also recover your container layout lista_cronograma. Something like:

ConstraintLayout rootCL = (ConstraintLayout) listItem.findViewById(R.id.root_cl);

And where you need it, change the background color normally:

rootCL.setBackgroundColor(Color.BLUE);

0

make a reference to your root element layout:

LinearLayout linearRoot = view.findViewById(R.id.linearRoot);

Now it will be possible to have control over your properties:

linearRoot.setBackgroundColor(Color.WHITE);

or...

linearRoot.setBackgroundColor(Color.parseColor("#ffff00"));

Browser other questions tagged

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