简体   繁体   中英

Android: How do I bind each value of an ArrayList to a variable?

I have an ArrayList that holds ImageViews:

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

I pass this ArrayList to my custom class so it can access each ImageView passed in.

CustomClass cClass = new CustomClass(ivArray);  

How can I get each value from the passed in ArrayList in my custom class, and bind them to their own variable?
I was thinking something along the lines of using a foreach loop, like:

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

Use a 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);
        }
    }

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