2013-02-17 57 views
0

所以我想出这个代码图片内容就不会出现

Map<Integer, Integer> images = new HashMap<Integer, Integer>();  
    images.put(1,R.drawable.a);  
    images.put(2,R.drawable.b);  
    images.put(3,R.drawable.c); 

String[] abcd = {"a","b","c"}; 
    Integer count = 3; 
    for(int inte = 0; inte==count;inte ++){ 
     if(strn.get(inte).equalsIgnoreCase(abcd[inte])){   
      image.setImageDrawable(getResources().getDrawable(images.get(inte)));   
     } 
    } 
  1. 把图像从可绘与整数键的HashMap
  2. 我提出的阵列[]与用户比较输入,for循环遍历hashmap的内容,如果条件为真,则显示图像。

这是我想要做的事情的见解,但... 现在我的问题是图像不会出现在我的代码之前。我认为我的问题有点类似于loop through hashtableCan't See Contents,并注意到EnumerationIterator,但无法设法将它们应用到我的代码中。有人可以指导我或任何建议都可以解决我的问题。

+0

在这个问题上接受的答案将帮助你:[从ArrayList HashMap获取多个随机值](http://stackoverflow.com/questions/14914062/get-multiple-random-values-from-arraylist-hashmap/14914068 #14914068) – jlordo 2013-02-17 16:44:43

回答

0

从我的回答here改编:

变化

for(int inte = 0; inte==count; inte++){ 
// start with inte beeing 0, execute while inte is 3 (count is 3) 
// never true 

说明:

for循环的结构如下:

for (initialization; condition; update) 

initialization在循环开始之前执行一次。在循环的每次迭代之前检查condition,并且在每次迭代之后执行update

您的initializationint inte = 0;(执行一次)。您的conditioninte == count,这是错误的,因为inte0count3。所以条件是false,并且循环被跳过。

+0

哦......对不起,我的循环不合逻辑,谢谢你纠正它先生,但仍然是我的主要问题仍然没有解决 – 2013-02-17 17:28:28

+0

@CharlesCasem:你的if条件是否评估为真?你能显示'strn'的内容是什么吗? – jlordo 2013-02-17 17:34:32

+0

,如果我走''的循环for'外if'语句返回true,则调整代码有点像这样'如果(strn.get(0).equalsIgnoreCase(ABCD [0])){\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t image.setImageDrawable(getResources()getDrawable(images.get(1))); \t \t}''我想我的问题在于'for'循环以及'setImageDrawable'如何获得'images'的关键字 – 2013-02-17 17:49:41