2014-02-28 122 views
-1

中的字符串将图像设置为imageView我在我的XML文件(tutor.xml)中有一组图像名称。
我想解析它并获取我的图像名称,并将其设置为ImageView在我的活动中。
它给出数据类型错误(不适用于图像)它必须是一个整数。无法通过android

try { 
    getAlphabet = getFromXML(MainActivity.this); 
    } catch (XmlPullParserException e) { 
    e.printStackTrace(); 
    } catch (IOException e) { 
    e.printStackTrace(); 
} 
     String[] str = getAlphabet.split("\n"); 
     int lenAcAlp = str.length; 
    String[] textFileLink = new String[lenAcAlp]; 
    String res = "R.drawable."; 
    int key=0; 
    while(key<lenAcAlp){ 
     textFileLink[key] = res + str[key]; 
    } 
    ImageView iv = (ImageView)findViewById(R.id.imageView1); 
    for(int i=0;i<lenAcAlp;i++){ 
     iv.setImageResource(textFileLink[i]); 
    } 
    } 

private String getFromXML(MainActivity mainActivity)throws XmlPullParserException, IOException { 
    StringBuffer stringBuffer = new StringBuffer(); 
     Resources res = mainActivity.getResources(); 
    XmlResourceParser xpp = res.getXml(R.xml.tutor); 
    xpp.next(); 
    int eventType = xpp.getEventType(); 
    while(eventType != XmlPullParser.END_DOCUMENT){ 
     if(eventType == XmlPullParser.START_TAG){ 
      if(xpp.getName().equals("tutorappdata")){ 
      stringBuffer.append(xpp.getAttributeValue(null, "keyitem") +"\n");    } 
     } 
     eventType = xpp.next(); 
    } 
    return stringBuffer.toString(); 
    } 

请大家帮帮我:我应该怎么设置我的图片到ImageView的㈣,如果不通过字符串应如何设置我的图片。
感谢

+0

的setImageResource()接受int类型的参数。您正在传递一个字符串。 int值应该指向资源文件夹 –

回答

2
R.drawable.xxx 

在Android中该语句将返回图像的INT-ID,而不是字符串

在ImageView的setImageResource(INT) - 设置绘制因为这样的ImageView的内容。

检查:http://developer.android.com/reference/android/widget/ImageView.html

在你的情况,请尝试此代码

for(int i=0;i<lenAcAlp;i++){ 
     int id = getResources().getIdentifier(textFileLink[i], "drawable", getPackageName()); 
     iv.setImageResource(id); 
} 
+0

中的可绘制资源,我知道,但任何解决方案。因为我有156张图片要显示,所以我无法在数组中进行管理。 –

+0

您的项目中的所有资源图片都可以通过上层功能加载,您不需要管理它 –

0

你可以把所有的图像资源ID为int数组......像这样;

int[] images = new int[]{R.drawable.xx,R.drawable.xxx,R.drawable.xxxxx}; 
for(int i=0;i<images.length();i++){ 
    iv.setImageResource(images[i]); 
} 
0

对我来说是很好的方法是创建一个ImageView的适配器(例如:http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/

然后在你的活动解析方法被称为成的OnCreate活动,填补一个ArrayList

然后感谢您的适配器和ArrayList你可以设置你的形象,你的ImageView的(这里的代码示例部分):

ArrayList<YourObjectImage> _filePaths; 

//Adapter constructor 
public ImageViewAdapter(Activity activity, ArrayList<YourObjectImage> filePaths) { 
     this._activity = activity; 
     this._filePaths = filePaths; 

    } 

[Code here check my link above] 

ImageView yourImage = (ImageView) imageView.findViewById(R.id.imageHere); 

Image img = (Image) _filePaths.get(position); //here image is your object     "image" in your case 

if(yourImage != null){ 
    yourImag.setImageResource(img.getId()); 
} 
0

请将此代码用于帮助您

int id = getResId(“app_icon”);

iv.setImageResource(id);

公众诠释getDrableId(字符串VARIABLENAME){

try { 
     Class res = R.drawable.class; 
     Field idField = res.getDeclaredField(variableName); 
     return idField.getInt(idField); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     return -1; 
    } 
}