I would love to know how to shrink this code into 1 line with a ternator operator, the idea is change the drawable of a image inside my RecyclerView when its 1 or 0, I have done this but I think is not that performant to have multiple if elses to do it, instead I know that it can be done in just 1 line with a ternary operator.
if(json.hasTime(mArrayList.get(mPosition))==1){
mHolder.imageEvent.setImageResource(R.drawable.ic_event_black_24dp);
}else{
mHolder.imageEvent.setImageResource(R.drawable.ic_event_busy_black_24dp);
}
I want to do something like this :
(if 1 or 0 ? value 1 : value2 );
尝试这个 :
mHolder.imageEvent.setImageResource(json.hasTime(mArrayList.get(mPosition))==1 ? R.drawable.ic_event_black_24dp : R.drawable.ic_event_busy_black_24dp);
You can always convert expressions from (1) to (2) like so,
//(1)
if(condition){
stmt1;
}else{
stmt2;
}
//(2)
somevariable = condition?stmt1:stmt2;
So your single line expression can be written as follows,
mHolder.imageEvent.setImageResource(json.hasTime(mArrayList.get(mPosition))==1?R.drawable.ic_event_black_24dp:R.drawable.ic_event_busy_black_24dp);
Something like this.
mHolder.imageEvent.setImageResource(json.hasTime(mArrayList.get(mPosition))==1?
R.drawable.ic_event_black_24dp : R.drawable.ic_event_busy_black_24dp)
尝试这个:
json.tieneHora(mArrayList.get(mPosition))==1?mHolder.imageEvent.setImageResource(R.drawable.ic_event_black_24dp):mHolder.imageEvent.setImageResource(R.drawable.ic_event_busy_black_24dp);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.