我想在列表视图中显示图像,其中图像存储在drawble文件夹中,我试图通过XML读取这里是我的XML代码。
<music>
<song>
<title>Live Shows</title>
<thumb_url>@drawable/chat.png</thumb_url>
</song>
<song>
<title>Space Bound</title>
<thumb_url>@drawable/email_open.png</thumb_url>
</song>
请帮帮我,我是android新手。
答案 0 :(得分:1)
解析xml并调用下面给出的函数
// imagePath - value of <thumb_url>@drawable/chat.png</thumb_url>
// imageView - view where image needs to be set
private void setImage(String imagePath,ImageView imageView)
{
String uriFromXML = imagePath;
String suffix = ".png";
if ((uriFromXML != null) && (uriFromXML.startsWith("@")) && (uriFromXML.endsWith(suffix)))
{
int length = uriFromXML.length();
int suffixLength = suffix.length();
String uri = uriFromXML.substring(1, length - suffixLength);
int imageResource = getResources().getIdentifier(uri, null,getPackageName());
Drawable image = getResources().getDrawable(imageResource);
imageView.setImageDrawable(image);
}
}
答案 1 :(得分:0)
好吧,我认为你正在使用androidhive tut。在惰性适配器类中,将此代码imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
更改为thumb_image.setImageResource(song.get(CustomizedListView.KEY_THUMB_URL));
答案 2 :(得分:0)
在Xml中试试这个
<music>
<song>
<title>Live Shows</title>
<thumb_url>chat</thumb_url>
</song>
<song>
<title>Space Bound</title>
<thumb_url>email_open</thumb_url>
</song>
和活动,
int redId = getResources().getIdentifier("<your_package>:drawable/<resource_name>", null, null);
imageView.setBackgroundDrawable(getResources().getDrawable(resId));
com.example.package
和<resource_name>
之类的内容将由您的xml中的thumb_url
填充。