中的字符串将图像设置为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的㈣,如果不通过字符串应如何设置我的图片。
感谢
的setImageResource()接受int类型的参数。您正在传递一个字符串。 int值应该指向资源文件夹 –