您的位置:首页 > 移动开发 > Android开发

安卓学习之使用listview设置adapter时,解决动态显示图片的问题

2017-01-25 19:45 531 查看

1.一文件名为item_parkinglot_infor_adapter.xml的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>

<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="@+id/padding_lot_img"
android:layout_marginLeft="20dp"/>

<LinearLayout
android:layout_marginLeft="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_padding_lot"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@
4000
+id/fee_scale"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/free_time"/>

</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@drawable/border_divider"
android:layout_marginLeft="20dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_marginLeft="20dp"
android:text="收藏次数"
android:id="@+id/id_collect_number"
android:gravity="center_vertical"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="更多信息"
android:textColor="@color/black"
android:textSize="14sp"
android:gravity="center_vertical"
android:layout_toLeftOf="@+id/id_right"
/>
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/img_right"
android:layout_alignParentRight="true"
android:id="@id/id_right"
/>
</RelativeLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="@drawable/border_divider"/>

</LinearLayout>
</LinearLayout>
<!--停车场推荐的listview的adapter-->

<!--我的收藏的listview的adapter-->
2.为listview添加数据
/**
* 将停车场的数据封装成List<Map<String,String>>
* @param string
* @return
*/
public List<Map<String ,Object >> enclosure(String string){
JSONObject data= null;
try {
data = new JSONObject(string);
} catch (JSONException e) {
e.printStackTrace();
}
Iterator key=data.keys();
//将数据封装在padding_lot_list中
List<Map<String ,Object>> padding_lot_list=new ArrayList<>();
while (key.hasNext()){
JSONObject item= null;
try {
item = new JSONObject(data.getString(""+key.next()));
Map<String ,Object> map=new HashMap<>();
map.put("parking_image","停车场图片:"+item.getString("parking_image"));
map.put("padding_lot_img",aCache.getAsBitmap(item.getString("parking_image").trim()));
map.put("id_padding_lot","停车场名:"+item.getString("parking_name"));
map.put("fee_scale","收费标准:"+item.getString("freight_basis"));
map.put("free_time","预定时间限制:"+item.getString("reserve_time"));
map.put("car_number","总车位数:"+item.getString("car_number"));
map.put("collect_number","收藏次数:"+item.getString("reserve_time"));
map.put("emptyCarSpace","空的停车位:"+item.getString("reserve_time"));
padding_lot_list.add(map);
} catch (JSONException e) {
e.printStackTrace();
}
}
return padding_lot_list;

}
3.设置adapter
SimpleAdapter padding_lot_adapter=new SimpleAdapter(context,list,R.layout.item_parkinglot_infor_adapter,new String[]{"padding_lot_img","id_padding_lot","fee_scale","free_time","collect_number"},new int[]{R.id.padding_lot_img,R.id.id_padding_lot,R.id.[b]fee_scale,R.id.free_time,R.id.id_collect_number});
padding_lot_adapter.setViewBinder(new SimpleAdapter.ViewBinder() {

@Override
public boolean setViewValue(View view, Object data,
String textRepresentation) {
if ( view instanceof ImageView && data instanceof Bitmap ) {
ImageView iv = (ImageView) view;
iv.setImageBitmap((Bitmap) data);

return true;
}
return false;
}
});
padding_lot_infor.setAdapter(padding_lot_adapter);
4.在解决该问题中最重要的就是部分就是调用setViewBinder()的这样的一个方法,通过查看代码可以得知,在设置完适配器时,便调用了setViewBinder()方法,通过判断控件是否是ImageView的实例以及数据是否是Bitmap的事例,然后在为ImageView控件设置图片。
5.效果如下

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android listview
相关文章推荐