Pie*_*nan 5 android android-widget
我想在ListView中显示新闻(来自RSS).我已经成功创建了ListView,正确显示了标题和描述.但是我无法从URL显示图像.
这是我的代码:
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
ArrayList <HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map;
int i = 0;
while (i < 55)
{
map = new HashMap<String, String>();
map.put("titre", newsArray[i]);
map.put("description", newsArray[i+1]));
map.put("img", newsArray[i+4]);
listItem.add(map);
i += 5;
}
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem,
R.layout.affichageitem, new String[] {"img", "titre", "description"},
new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
Run Code Online (Sandbox Code Playgroud)
你可以猜到,newsArray[i+4]包含我的图像的URL.
我写了一个函数drawable_from_url,它返回一个android.graphics.drawable.Drawable并且工作正常.我试着写这个:
map.put("img", String.valueOf(drawable_from_url(newsArray[i+4], "newsPic")));
Run Code Online (Sandbox Code Playgroud)
但它也没有工作(没有显示图像).价值的一个例子
String.valueOf(drawable_from_url(newsArray[i+4], "newsPic"))
可能是
android.graphics.drawble.BitmapDrawable@481f16d0
任何的想法 ?
谢谢.
尝试这个方法从 url 加载位图
public Bitmap setRemoteImage(URL url) {
try {
Bitmap bm=null;
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis=new BufferedInputStream(is);
try {
bm = BitmapFactory.decodeStream(bis);
} catch (Exception e) {
// TODO Auto-generated catch block
MainActivity mm= new MainActivity();
Drawable dra=mm.getResources().getDrawable(R.drawable.icon);
Bitmap bb=((BitmapDrawable)dra).getBitmap();
bm=bb;
}
is.close();
return bm;
}catch (IOException e) {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1693 次 |
| 最近记录: |