Reputation: 45
I want to change color of my cardview
in Recyclerview
if a string matches a string from QR code. I am able to display a toast if string matches, but I also want to change color of that particular element in Recyclerview.
here is the code of selecting an item for scanning
recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(getApplicationContext(), new RecyclerItemClickListener.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
tempString = passengerDataModelList.get(position).getPnr();
tempClickPosi = position;
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
startActivityForResult(intent, 0);
}
}));
Now on ActivityResult I will get the result:
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
if(contents.equalsIgnoreCase(tempString)){
Toast.makeText(this, "String MATCHED", Toast.LENGTH_SHORT).show();
///// Here I want to change the color of that item in recycler view
}else if(!contents.equalsIgnoreCase(tempString)){
Toast.makeText(this, "String NOT MATCHED", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this, "Error in scanning", Toast.LENGTH_SHORT).show();
}
}
}
}
So please help me to change the color of that particular item from activity. If need more info kindly comment. Thank You
Upvotes: 0
Views: 844
Reputation: 12953
For suppose you have a model
public class PassengerData{
//add this property
private boolean isMatched = false;
void setMatched(boolean matched){
this.isMatched = matched;
}
boolean isMatched(){
return isMatched;
}
}
Then in recyclerview's
bindview
just verify the boolean,
boolean matched = passengerDataModelList.get(position).isMatched();
cardview.setBackgroundColor(matched ? R.color.matched_color : R.color.unmatched_color);
Then in Activity
if(contents.equalsIgnoreCase(tempString)){
Toast.makeText(this, "String MATCHED", Toast.LENGTH_SHORT).show();
///// Here I want to change the color of that item in recycler view
// modify `isMatched` value for that item and call `notifyDataSetChanged`();
}
Upvotes: 1
Reputation: 402
According to my understanding, I guess You want to make background colorful if String is match. In RecyclerView you are inflating rows so your rows must have a Layout which You can use like below if your string is matched. The below code is only for understanding purpose. Customize it accordingly. Here I am changing color of Recycler Item, based on condition so i wrote this condition. You can modify it
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
if(position==0){
holder.itemView.setBackgroundColor(Color.BLACK);
}else{
holder.itemView.setBackgroundColor(Color.GREY);
}
holder.mTextView.setText(mDataArray.get(position));
}
Upvotes: 0
Reputation: 1275
Create separate flag for color and then once string is matched update flag for particular item and then notify your adapter
Upvotes: 0