简体   繁体   English

android字典int,Imageview

[英]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 我无法将对象放入字典中并得到它们,我想将imageViews放在int上,但是失败而没有任何错误

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. 您的更新更有意义,但是Dictionary类仍然过时。 From the documentation : 文档中

java.util Class Dictionary java.util类字典
... ...
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. 由于您要创建一个由Integers索引的表( new Dictionary<Integer, ImageView>() ),我们应该使用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. 同样,当您使用setTag() ,会将Integer转换为Object,并且每次使用getTag()都会将Integer从Object转换回。 This works, but if you can use getId() it will be faster. 这可以工作,但是如果可以使用getId() ,它将更快。

I recommend this: 我建议这样做:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ImageView 到 int Android Studio 的图像 - Image of an ImageView to int Android Studio 空对象引用上的“void android.widget.ImageView.setVisibility(int)” - 'void android.widget.ImageView.setVisibility(int)' on a null object reference 将Int转换为ImageView - Convert Int to ImageView 使用int更改imageview - change imageview using int NullPointerException:尝试在空对象引用上调用虚拟方法&#39;void android.widget.ImageView.setImageResource(int)&#39; - NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference 我尝试在空对象引用上调用虚拟方法&#39;void android.widget.ImageView.setImageResource(int)&#39; - I got Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference 在空引用上使 android.widget.imageview.setimageresource(int)&#39; 无效(日志猫中的致命错误消息) - Void android.widget.imageview.setimageresource(int)' on a Null Reference(fatal error message in the log cat) 根据int数组显示imageview - display imageview according to int array Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference - Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference Android ImageView无法清除 - Android ImageView wont clear
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM