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);
}
}