如果字符串与二维码中的字符串匹配,我想更改 Recyclerview
中 cardview
的颜色。如果字符串匹配,我可以显示 toast,但我也想更改 Recyclerview 中该特定元素的颜色。
这是选择扫描项目的代码
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);
}
}));
现在在 ActivityResult 我将得到结果:
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();
}
}
}
}
所以请帮助我更改 Activity 中该特定项目的颜色。如果需要更多信息请评论。 谢谢您
最佳答案
假设你有一个模型
public class PassengerData{
//add this property
private boolean isMatched = false;
void setMatched(boolean matched){
this.isMatched = matched;
}
boolean isMatched(){
return isMatched;
}
}
然后在recyclerview的
bindview
中验证 boolean 值,
boolean matched = passengerDataModelList.get(position).isMatched();
cardview.setBackgroundColor(matched ? R.color.matched_color : R.color.unmatched_color);
然后在 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`();
}
关于java - 如何更改回收 View 元素的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47712657/