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

时间:2013-10-24 12:35:01

标签: java android arraylist foreach imageview

我有一个包含ImageViews的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 个答案:

答案 0 :(得分:3)

使用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);
        }
    }