简体   繁体   中英

android dictionary int, Imageview

I am failing to put objects in dictionary and get them, I want to put imageViews against int but failing without any error

dictionary declared as:

    Dictionary<Integer, ImageView> dictPlayerAll = new Dictionary<Integer, ImageView>(){

        @Override
        public int size() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public ImageView remove(Object key) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public ImageView put(Integer key, ImageView value) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Enumeration<Integer> keys() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public boolean isEmpty() {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public ImageView get(Object key) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public Enumeration<ImageView> elements() {
            // TODO Auto-generated method stub
            return null;
        }
    };;

here putting values in dictionary

    int mTag = iv.getTag();//its my imageview
    dictPlayerAll.put(mTag, iv);

but it shows zero size

Your update makes more sense, but the Dictionary class is still obselete. From the documentation :

java.util Class Dictionary
...
Note: Do not use this class since it is obsolete.

Since you want to create a table indexed by Integers ( new Dictionary<Integer, ImageView>() ) we should use a SparseArray.

Also when you use setTag() you convert your Integer to an Object and every time you use getTag() you are converting your Integer back from an Object. This works, but if you can use getId() it will be faster.

I recommend this:

SparseArray<ImageView> allPlayers = new SparseArray<ImageView>();
...

allPlayers.put(iv.getId(), iv);

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