简体   繁体   中英

Can a variable have two values?

可以给变量两个值吗?

ImageView imageView  = itemView.findViewById(R.id.imageView1) && itemView.findViewById(R.id.imageView2);

I think you might be looking for an or solution instead of an and . An and of views makes no sense.

However, you could default to a second view if a first view cannot be found:

imageView = Optional.ofNullable(itemView.findViewById(R.id.imageView1))
                    .orElse(itemView.findViewById(R.id.imageView2));

Short answer: NO

but, it depends where, you can manage to do different things:

Pair<ImageView, ImageView>  imageViewPair  = new Pair<>(itemView.findViewById(R.id.imageView1), itemView.findViewById(R.id.imageView2));

that could be an option, if you want multiple:

List<ImageView> images = Arrays.asList(itemView.findViewById(R.id.imageView1), itemView.findViewById(R.id.imageView2), ..., ...);

and also with Optional as @Saswat Padhi you have another one way to do it.

In this case - no. However it is possible to hold multiple values in variable that is a collection.

不,您不能这样做,应该一次定义图像视图,如果您需要轻松的方法来绑定视图,则可以使用黄油刀单击此处。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM