如何动态添加图像到图像查看

问题描述:

我只是想知道你是否有人知道如何做到这一点。我有一个有关键值对的活动,如果关键字等于一个字符串,我试图显示一个图像,否则不显示任何图像。如何动态添加图像到图像查看

有谁知道如何或从哪里开始试图让这个工作正确吗?

final String splitedDouble[] = companyString.split(",,"); 

    String[] arrayOfString = { "Beard Vape Co;beard_vape_co", "two;zwei", "three;drei" }; 

    Hashtable<String, String> hashTable = new Hashtable<String, String>(); 
    for(String s: arrayOfString){ 
     String[] array = s.split(";"); 
     String sKey ="", sValue=""; 
     if(array.length > 1){ 
      sKey = array[0]; sValue = array[1]; 
      hashTable.put(sKey, sValue); 
      if(sKey.toString().equals(liquidCompany)){ 
       imageView.setImageResource(Integer.parseInt("R.drawable." + sValue.toString())); 
      } 
     } 

这就是我对循环

+0

添加一些代码来显示你在做什么。顺便说一句,你可以使用View.setVisibility()来显示/隐藏你的ImageView。 –

+0

好听起来很好对不起,我得到了循环了,我试图让工作,但迄今没有运气 –

+0

好吧。它有什么问题? –

所以这是解决这个我是有我知道它不是最漂亮的还是最好的代码,但它有一个我一直在寻找的功能的问题。感谢大家对这个问题的所有帮助!

String[] arrayOfString = { "Beard Vape Co.;beard_vape_co", "two;zwei", "three;drei" }; 

    List<String> sKeyArray = new ArrayList<>(); 
    List<String> sValueArray = new ArrayList<>(); 
    for(String s: arrayOfString) { 
     String[] array = s.split(";"); 
     String sKey = "", sValue = ""; 
     if (array.length > 1) { 
      sKey = array[0]; 
      sValue = array[1]; 
      sKeyArray.add(sKey); 
      sValueArray.add(sValue); 
     } 

    } 
    for(int i = 0; i < sKeyArray.size(); i++){ 
     if(sKeyArray.get(i).toString().contains(liquidCompany.trim().toString())){ 
      testingText.append(sKeyArray.get(i).toString()); 
      testingText.append(sValueArray.get(i).toString()); 
      int resID = getResources().getIdentifier(sValueArray.get(i).toString() , "drawable", getPackageName()); 
      imageView.setImageResource(resID); 
     } 
    } 
+0

的帮助所以基本上它只是错误的代码,你需要帮助调试。下次尝试自己隔离问题。问问自己什么是错的,然后测试一下。在这里,它可能是'for'循环,'if'测试或者传递给'setImageResource'的resID。在***之后,您最好使用* ***测试了代码的每个方面,并确定哪些***行不按照您期望的方式工作。那么你会有一个*特定的*问题,其答案*会对其他人有用*。在这里,你所拥有的是“我的代码坏了,我不知道为什么”。解决它只会帮助你。 – ToolmakerSteve

如果你已经有了一个ImageView的设置你的布局,您可以使用View.setVisibility显示或隐藏的ImageView:

ImageView image = findViewById(R.id.image); 
if (key.equals("String"))  
    image.setVisibility(View.VISIBLE); 
else 
    image.setVisibility(View.GONE); 

如果您想在更新图像后更改图像,可以使用

image.setImageResource(R.drawable.whatever); 

这是c Lassic案件的XY问题。你真正的问题是从字符串获取资源ID。这已经在SO之前很多时候被回答了。 link

试试这个:

final String splitedDouble[] = companyString.split(",,"); 

String[] arrayOfString = { "Beard Vape Co;beard_vape_co", "two;zwei", "three;drei" }; 


Hashtable<String, String> hashTable = new Hashtable<String, String>(); 
for(String s: arrayOfString){ 
    String[] array = s.split(";"); 
    String sKey ="", sValue=""; 
    if(array.length > 1){ 
     sKey = array[0]; sValue = array[1]; 
     hashTable.put(sKey, sValue); 
     imageView.setImageResource(getResId("R.drawable." + sValue.toString(), Drawable.class)); 
     imageView.setVisiblity(View.GONE); 
     if(sKey.toString().equals(liquidCompany)){ 
      imageView.setVisiblity(View.VISIBLE); 
     } 
    } 

public static int getResId(String resName, Class<?> c) { 
    try { 
     Field idField = c.getDeclaredField(resName); 
     return idField.getInt(idField); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return -1; 
    } 
} 
+0

有了这个,当我把它放到我的android工作室时,有很多东西我无法解决或导入 –

+0

是真的。这不是我测试过的。另外我还没有添加任何进口。所以我可以想象它有一些未解决的符号。但我希望它能给你足够的信息来指出问题并达成解决方案。 –

你的形象并未出现的原因是因为这里

imageView.setImageResource(Integer.parseInt("R.drawable." + sValue.toString())); 

你没有设置你的图像的正确的资源ID。 Integer.parseInt("R.drawable." + sValue.toString())返回无效标识符。

你可以按照下面的例子来做。

Class<?> drawableClass = R.drawable.class; 
    try { 
     Field icLauncherField = drawableClass.getDeclaredField("ic_launcher"); 
     imageView.setImageResource(Integer.valueOf(icLauncherField.getInt(null))); 
    } catch (NoSuchFieldException | IllegalAccessException | IllegalArgumentException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
+0

这仍然会进入循环? –

+0

是的,你可以在循环内进行。 – alijandro

+0

好听起来不错我会试试它真的很快。谢谢大家对这个 –