使用simpleAdapter与图像列表视图 [英] Using simpleAdapter with image for listview
问题描述
我有一个小问题,把图像利用简单的适配器列表视图。我是从我的网络服务器(AMAZON)获取图像。基于用户ID下载图像后,我尝试设置他们在我的列表视图但是什么也没有显示,也没有错误发生。
下面是我的code:
//遍历所有申请
的for(int i = 0; I< applicant.length();我++){
JSONObject的C = applicant.getJSONObject(我);
//保存在变量中的每个JSON项目
字符串的uid = c.getString(TAG_UID);
字符串名称= c.getString(TAG_NAME);
字符串整体= c.getString(TAG_OVERALL);
字符串apply_datetime = c.getString(TAG_APPLY_DATETIME);
字符串的照片= c.getString(TAG_PHOTO);
//创建新的HashMap
// HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
//图像
HashMap的<字符串,对象>图=新的HashMap<字符串,对象>();
//添加每个子节点HashMap的键(值)
map.put(TAG_UID,UID);
map.put(TAG_NAME,姓名);
map.put(TAG_OVERALL,整体);
map.put(TAG_APPLY_DATETIME,apply_datetime);
//添加HashList到ArrayList中
// applicantsList.add(图)
//挂牌,形象TO LISTVIEW
尝试 {
IMAGEURL = c.getString(TAG_PHOTO);
InputStream的是=(InputStream的)新的URL(
我的URL链接/图片/
+ IMAGEURL).getContent();
D = Drawable.createFromStream(是,SRC名);
}赶上(例外五){
e.printStackTrace();
}
map.put(TAG_PHOTO,D);
//添加HashList到ArrayList中
applicantsList.add(图)
}
正如你所看到的,我下载后的图像。我使用下面simpleAdapter设置为列表视图:
SimpleAdapter适配器=新SimpleAdapter(
SignUpApplicantActivity.this,applicantsList,
R.layout.list_applicant,新的String [] {
TAG_UID,TAG_NAME,TAG_OVERALL,
TAG_APPLY_DATETIME,TAG_PHOTO},新的INT [] {
R.id.applicantUid,R.id.applicantName,
R.id.applicantOverall,
R.id.apply_datetime,R.id.list_image});
//更新的ListView
setListAdapter(适配器);
从 SimpleAdapter文档图像数据预计将资源ID或字符串(图像URI) - 见<一href="http://developer.android.com/reference/android/widget/SimpleAdapter.html#setViewImage%28android.widget.ImageView,%20java.lang.String%29"相对=nofollow> setViewImage(ImageView的,字符串)
我看到两个解决方案:
- 在地图数据提供一个URI,而不是绘制。
-
实现自己的观点粘合剂可绘绑定到ImageView的:
adapter.setViewBinder(新SimpleAdapter.ViewBinder(){ @覆盖 公共布尔setViewValue(查看视图,对象数据,字符串textRe presentation){ 如果(view.getId()== R.id.list_image){ ImageView的ImageView的=(ImageView的)观点; 可绘制绘制=(抽出式)的数据; imageView.setImageDrawable(绘制); 返回true; } 返回false; } });
I have a little problem with putting an image to a list view using simple adapter. I'm getting the image from my online server(AMAZON). After downloading the image based on the user id, i try to set them in my listview but nothing was display and no error is occured.
Below is my code:
// looping through All applicants
for (int i = 0; i < applicant.length(); i++) {
JSONObject c = applicant.getJSONObject(i);
// Storing each JSON item in variable
String uid = c.getString(TAG_UID);
String name = c.getString(TAG_NAME);
String overall = c.getString(TAG_OVERALL);
String apply_datetime = c.getString(TAG_APPLY_DATETIME);
String photo = c.getString(TAG_PHOTO);
// creating new HashMap
//HashMap<String, String> map = new HashMap<String, String>();
//IMAGE
HashMap<String, Object> map = new HashMap<String, Object>();
// adding each child node to HashMap key (value)
map.put(TAG_UID, uid);
map.put(TAG_NAME, name);
map.put(TAG_OVERALL, overall);
map.put(TAG_APPLY_DATETIME, apply_datetime);
// adding HashList to ArrayList
// applicantsList.add(map);
// LISTING IMAGE TO LISTVIEW
try {
imageURL = c.getString(TAG_PHOTO);
InputStream is = (InputStream) new URL(
"my url link/images/"
+ imageURL).getContent();
d = Drawable.createFromStream(is, "src name");
} catch (Exception e) {
e.printStackTrace();
}
map.put(TAG_PHOTO, d);
// adding HashList to ArrayList
applicantsList.add(map);
}
As you can see, after i download the image. i set to listview using simpleAdapter below:
SimpleAdapter adapter = new SimpleAdapter(
SignUpApplicantActivity.this, applicantsList,
R.layout.list_applicant, new String[] {
TAG_UID, TAG_NAME, TAG_OVERALL,
TAG_APPLY_DATETIME, TAG_PHOTO }, new int[] {
R.id.applicantUid, R.id.applicantName,
R.id.applicantOverall,
R.id.apply_datetime, R.id.list_image });
// updating listView
setListAdapter(adapter);
From the SimpleAdapter documentation the image data is expected to be a resource ID or a string (an image URI) - see setViewImage(ImageView,String)
I see 2 solutions:
- Provide a URI in the data map, not a drawable.
Implement your own view binder to bind the drawable to the ImageView:
adapter.setViewBinder(new SimpleAdapter.ViewBinder() { @Override public boolean setViewValue(View view, Object data, String textRepresentation) { if(view.getId() == R.id.list_image) { ImageView imageView = (ImageView) view; Drawable drawable = (Drawable) data; imageView.setImageDrawable(drawable); return true; } return false; } });
这篇关于使用simpleAdapter与图像列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!