0

UPDATE: I tried to create 3 different layouts, one with a green card, other with red, and other yellow. It still not working. All color remain the same. :(

I have a RFID device. When I read tags, it would populate a listview with cardviews which has the tag's code.

I have just 3 rules: If the tag read exists in a text file loaded into the device, the card will become green. If the tag read does not exists in the text file, it becomes yellow. And if I don't read a tag which code exists in the text file (in other words, if the tag is missing) the cardview will become red.

Everything is working. The RFID reader, the app, ok. But I simply cannot change the colors correctly.

Does anyone know how to do it? Looks simple, but I really can't figure it out.

I did a switch case, I tried if/else, but nothing worked. It simply changes all cards colors to the same one. It does not changing the color individually. Actually, the information inside the cardviews are correct! But I cannot understand why the colors does not change individually. All cards become with the same color, does not matter if they were read or not.

This is my adapter class. Please ignore the commented lines, they were all my tries...

public class NewAdapter extends BaseAdapter {

    private Context context;
    private List<PropsCard> cardProps1;
    //private RecyclerView.Recycler<PropsCard> cardProps2;

    public NewAdapter(Context context, List<PropsCard> cardProps) {
        this.context = context;
        this.cardProps1 = cardProps;
    }

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

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

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

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

        PropsCard cardProps = cardProps1.get(position);

        if (convertView == null) {

            for(com.example.compexrf.PropsCard card: cardProps1) {

                switch (card.cor) {
                    case 0:
                        //Red
                        //cd.setBackgroundColor(Color.parseColor("#F4BABA"));
                        //cdview_red.setCardBackgroundColor(Color.RED);
                        //bt.setBackgroundColor(Color.RED);
                        //CardView cdview_red = (CardView) convertView.findViewById(R.id.cdviewred);
                        //convertView.setBackgroundColor(Color.parseColor("#F4BABA"));
                        //convertView = View.inflate(context, R.layout.card_itens, null);
                        convertView = LayoutInflater.from(context).inflate(R.layout.card_itens, null);
                        //convertView.setBackgroundColor(Color.RED);

                        break;
                    case 1:
                        //Yellow
                        //cd.setBackgroundColor(Color.parseColor("#FCECA4"));
                        //cdview_yellow.setBackgroundColor(Color.YELLOW);
                        //CardView cdview_yellow = (CardView) convertView.findViewById(R.id.cdviewyellow);
                        //convertView.setBackgroundColor(Color.parseColor("#FCECA4"));
                        //convertView = View.inflate(context, R.layout.card_itens2, null);
                        convertView = LayoutInflater.from(context).inflate(R.layout.card_itens2, null);
                        //convertView.setBackgroundColor(Color.YELLOW);

                        break;
                    case 2:
                        //Green
                        //cd.setBackgroundColor(Color.parseColor("#5FDDC1"));
                        //cdview_green.setBackgroundColor(Color.GREEN);
                        //CardView cdview_green = (CardView) convertView.findViewById(R.id.cdviewgreen);
                        //convertView.setBackgroundColor(Color.parseColor("#5FDDC1"));
                        //convertView = View.inflate(context, R.layout.card_itens3, null);
                        convertView = LayoutInflater.from(context).inflate(R.layout.card_itens3, null);
                        //convertView.setBackgroundColor(Color.GREEN);

                        break;
                    default:
                        break;
                }

            }
        }

        TextView desc_txt = (TextView) convertView.findViewById(R.id.descricao);
        TextView cod_txt = (TextView) convertView.findViewById(R.id.codigoRFID);
        ImageView imageView = (ImageView) convertView.findViewById(R.id.image);
        //RelativeLayout relativeLayout = (RelativeLayout) convertView.findViewById(R.id.relative);
        CardView cd = (CardView) convertView.findViewById(R.id.cdviewred);
        CardView cd2 = (CardView) convertView.findViewById(R.id.cdviewyellow);
        CardView cd3 = (CardView) convertView.findViewById(R.id.cdviewgreen);

        //Button bt = (Button) convertView.findViewById(R.id.botao);

        desc_txt.setText(cardProps.desc);
        cod_txt.setText(cardProps.id);

        if (cardProps.desc.contains("Controlador")) {
            imageView.setImageResource(R.drawable.quadro);
            //relativeLayout.setBackgroundColor(Color.parseColor("F4BABA"));
        }

        else if (cardProps.desc.contains("Quadro")) {
            imageView.setImageResource(R.drawable.quadro);
            //relativeLayout.setBackgroundColor(Color.parseColor("FCECA4"));
        }

        else if (cardProps.desc.contains("Quadro")) {
            imageView.setImageResource(R.drawable.quadro);
        }

        else if (cardProps.desc.contains("Câmera de Validação Esteira")) {
            imageView.setImageResource(R.drawable.quadro);
        }

        else if (cardProps.desc.contains("Medicamentos")) {
            imageView.setImageResource(R.drawable.med);
            //relativeLayout.setBackgroundColor(Color.parseColor("5FDDC1"));
        }

        else if (cardProps.desc.contains("Impressora")) {
            imageView.setImageResource(R.drawable.printer01);
        }

        else {
            imageView.setImageResource(R.drawable.cpx);
        }

        return convertView;

    }

This is another class to help to what I need:

public class PropsCard implements Comparable<PropsCard> {

    String id;
    String desc;
    int cor;

    public PropsCard(String id, String desc, int cor){
        this.id = id;
        this.desc = desc;
        this.cor = cor;
    }

    public PropsCard(String id, int cor){
        this.id = id;
        this.desc = "";
        this.cor = cor;
    }

    @Override
    public boolean equals (Object object){
        if(object != null && object instanceof PropsCard){
            PropsCard obj = (PropsCard) object;
            return id.equals(obj.id);
        }
        return false;
    }

    @Override
    public int compareTo(PropsCard cdProp){
        if(cor > cdProp.cor)
            return 1;
        else if(cor == cdProp.cor)
            return 0;
        else
            return -1;
    }
}

Ans this is my method of RFID reading:

public void onScanCompleted(String code, String rssi, int type) {
            PropsCard rdCard = new PropsCard(code, 0);
            if(!cardList.contains(rdCard)){
                rdCard.cor = 1;
                cardList.add(rdCard);
            }
            else {
                int idx = cardList.indexOf(rdCard);
                rdCard = cardList.get(idx);
                if(rdCard.cor == 0) {
                    rdCard.cor = 2;
                    cardList.set(idx, rdCard);
                }
            }
            if(!ScanAndUhf.getHasData())
                makeCards(cardList);
        }
}
Shahbaz Hashmi
  • 2,631
  • 2
  • 26
  • 49
Vhox
  • 17
  • 7

2 Answers2

0

To change CardView background colour, you should use card.setCardBackgroundColor(color); instead of cd.setBackgroundColor(Color.parseColor("#FCECA4"));

  • Hello Robert. Thanks for your answer. I already tried this aswell. When it populates my listview, all cards become the same color. Looks like that it takes the color of the last cardview and put it to all others. I am really stuck on this. – Vhox Jul 10 '19 at 17:58
  • I noticed also that you do a for loop of all cards when you are checking `switch(card.cor)` instead of the card item for the current position like `cardProps.cor` ? – Róbert Polovitzer Jul 10 '19 at 18:20
0

I would recommend you to use a custom adapter and use a drawable.

You can see something like that in this Example

Although it changes on click, you just have to implement your logic, but it's going to be something like this:

public void onBindViewHolder(MyViewHolder holder, int position) {
if(youritem==somevalue)
   holder.view.setBackgroundColor(Color.GREEN);
else if(youritem==anothervalue)
   holder.view.setBackgroundColor(Color.parseColor("#somecolor"));
}

I use something like this to change text colors all the time:

@Override
public void onBindViewHolder(OSCardAdapter.MyViewHolder holder, int position) {

    OSActivity OS = OSListFiltered.get(position);

if(holder.status.getText().toString().equals("CLOSED"))
        {
            holder.status.setTextColor(Color.RED);
        }
}

You can use it to change the text color according to your background changes.

Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Douglas Cunha
  • 164
  • 2
  • 8