有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Android:如何将ArrayList的每个值绑定到一个变量?

我有一个保存ImageView的ArrayList:

ArrayList<ImageView> ivArray = new ArrayList<ImageView>();  
imgView1 = (ImageView) findViewById(R.id.imgView1);  
imgView2 = (ImageView) findViewById(R.id.imgView2);  
ivArray.add(imgView1);  
ivArray.add(imgView2);  

我将此ArrayList传递给自定义类,以便它可以访问传入的每个ImageView

CustomClass cClass = new CustomClass(ivArray);  

如何从自定义类中传入的ArrayList中获取每个值,并将它们绑定到自己的变量
我在考虑使用foreach循环,比如:

public class CustomClass() {
    public CustomClass(ArrayList<ImageView> ivArray) {
        for (ImageView iView : ivArray) {
            // Bind each ImageView
        }
    }
}

共 (1) 个答案

  1. # 1 楼答案

    使用HashMap

     public class CustomClass{
            //Bind it to a string just for example
            private HashMap<String, ImageView> mMap = new HashMap<String, ImageView>();
            public CustomClass(ArrayList<ImageView> ivArray) {
                for (ImageView iView : ivArray) {
                    mMap.put("SomeValue", iView);
                }
            }
            public ImageView getImageViewByString(String s){
                return mMap.get(s);
            }
        }