[英]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.