Android Studio ListView列表绑定网络JSON数据
来源:互联网 发布:网络歌手什么意思 编辑:程序博客网 时间:2024/06/17 05:34
效果显示:
1、创建xml页面文件(ListView数据列表载体)
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!--1. 顶部标题栏--> <RelativeLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/encode_view" android:orientation="horizontal" android:id="@+id/relativeLayout"> <TextView android:id="@+id/doctor_tv_headline " android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="18dp" android:text="查看医生" android:textSize="24sp" android:textStyle="bold" /> <ImageView android:id="@+id/doctor_iv_return" android:layout_width="30dp" android:layout_height="30dp" android:layout_centerVertical="true" android:layout_marginLeft="10dp" android:src="@android:drawable/ic_menu_revert" /> </RelativeLayout> <ListView android:id="@+id/doctor_lv_list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/relativeLayout"> </ListView></RelativeLayout>
2、创建xml数据绑定载体
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp"> <!--方形头像--> <ImageView android:id="@+id/doctor_lv_headPortrait" android:layout_width="80dp" android:layout_height="80dp" android:layout_centerVertical="true"/> <TextView android:id="@+id/doctor_tv_doctorName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/doctor_tv_sectionIntro" android:layout_alignStart="@+id/doctor_tv_sectionIntro" android:layout_alignTop="@+id/doctor_lv_headPortrait" android:ellipsize="end" android:maxLines="1" android:text="TextView" android:textColor="@color/black" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/doctor_tv_sectionName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/doctor_tv_doctorName" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignTop="@+id/doctor_tv_doctorName" android:layout_marginEnd="70dp" android:layout_marginRight="70dp" android:text="TextView" android:textSize="18sp" android:textStyle="bold" /> <TextView android:id="@+id/doctor_tv_sectionIntro" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginLeft="26dp" android:layout_marginStart="26dp" android:layout_toEndOf="@+id/doctor_lv_headPortrait" android:layout_toRightOf="@+id/doctor_lv_headPortrait" android:ellipsize="end" android:maxLines="2" android:text="list view item" android:textColor="@color/grey" android:textSize="14sp" /></RelativeLayout>
3、需要的JSON数据集合
4、创建一个接收数据实现类
package chenglong.activitytest.pengintohospital.entity;import org.json.JSONException;import org.json.JSONObject;import java.math.BigDecimal;/** * Created by LICHENGLONG on 2017-10-12. */public class BasSectionInfo { private Integer id;//医生ID private String headPortrait;//头像 private String doctorName;//姓名 private Integer sex;//性别(0-男,1-女) private Integer year;//年龄 private String workers;//工号 private Integer sectionId;//科室ID private BigDecimal basePay;//基本工资 private String phone;//电话 private String site;//地址 private String intro;//简介 private String sectionName;//科室名称 private String sectionIntro;//简介 public BasSectionInfo( Integer id, String headPortrait, String doctorName, Integer sex, Integer year, String workers, Integer sectionId, BigDecimal basePay, String phone, String site, String intro, String sectionName, String sectionIntro) { this.id = id; this.headPortrait = headPortrait; this.doctorName = doctorName; this.sex = sex; this.year = year; this.workers = workers; this.sectionId = sectionId; this.basePay = basePay; this.phone = phone; this.site = site; this.intro = intro; this.sectionName = sectionName; this.sectionIntro = sectionIntro; } public static BasSectionInfo sectionInfoData(JSONObject json){ try { return new BasSectionInfo( json.getInt("id"), json.getString("headPortrait"), json.getString("doctorName"), json.getInt("sex"), json.getInt("year"), json.getString("workers"), json.getInt("sectionId"), new BigDecimal(json.getString("basePay")).setScale(2, BigDecimal.ROUND_HALF_UP), json.getString("phone"), json.getString("site"), json.getString("intro"), json.getString("sectionName"), json.getString("sectionIntro")); } catch (JSONException e) { e.printStackTrace(); } return null; }}
5、获取JSON数据
(1)、创建接收参数
List<BasSectionInfo> listBasSectionInfoData = new ArrayList<>();
(2)、获取数据
/** * 获取JSON医生信息数据 */public void findSectionData(){ AsyncHttpClient client = new AsyncHttpClient(); client.get(AbAppConfig.DATA_URL + "appGVDoctor/findSectionData", null, new AsyncHttpResponseHandler() { @Override public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { try { JSONObject object = new JSONObject(new String(responseBody));//获取json数据 JSONArray jsonArray = object.getJSONArray("obj");//获取数据集名称为obj的数据 Log.d("jsonArray数据输出:", String.valueOf(jsonArray)); for (int i = 0; i < jsonArray.length();i++) { BasSectionInfo novels = BasSectionInfo.sectionInfoData(jsonArray.getJSONObject(i));//把数据存在novels集合中 if (novels != null){ listBasSectionInfoData.add(novels); }else { Toast.makeText(GV_Doctor.this, "数据为空!", Toast.LENGTH_LONG).show(); } }//数据加载完毕,通知列表去更新baseAdapter.notifyDataSetChanged(); } catch (JSONException e) { Toast.makeText(GV_Doctor.this, "数据请求失败,请稍后重试", Toast.LENGTH_SHORT).show(); } } @Override public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { //请求失败的回调处理 Toast.makeText(GV_Doctor.this, "请链接网络,稍后重试", Toast.LENGTH_SHORT).show(); } });}
(3)、在onCreate中调用
findSectionData();//获取数据
6、获取ListView控件
(1)、定义ListView参数
ListView doctor_lv_list;
(2)、获取控件
doctor_lv_list = (ListView)findViewById(R.id.doctor_lv_list);
7、绑定ListView的数据
(1)、创建BaseAdapter并获取数据
BaseAdapter baseAdapter = new BaseAdapter() { @Override public int getCount() { return listBasSectionInfoData.size(); } @Override public Object getItem(int position) { return listBasSectionInfoData.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { final SectionInfo sectionInfo; if (convertView == null){ convertView = getLayoutInflater().inflate(R.layout.reservation_gv_doctor_listview,null); sectionInfo = new SectionInfo(); sectionInfo.doctor_lv_headPortrait = (ImageView) convertView.findViewById(R.id.doctor_lv_headPortrait); sectionInfo.doctor_tv_doctorName = (TextView) convertView.findViewById(R.id.doctor_tv_doctorName); sectionInfo.doctor_tv_sectionName = (TextView) convertView.findViewById(R.id.doctor_tv_sectionName); sectionInfo.doctor_tv_sectionIntro = (TextView) convertView.findViewById(R.id.doctor_tv_sectionIntro); convertView.setTag(sectionInfo); } else { sectionInfo = (SectionInfo) convertView.getTag(); } final BasSectionInfo basSectionInfo = (BasSectionInfo)getItem(position); Log.d("novels数据:", String.valueOf(basSectionInfo)); ImageLoadProxy.getImageCache(GV_Doctor.this).get(AbAppConfig.IMAGES_URL+basSectionInfo.headPortrait, sectionInfo.doctor_lv_headPortrait); sectionInfo.doctor_tv_doctorName.setText(basSectionInfo.doctorName); sectionInfo.doctor_tv_sectionName.setText(basSectionInfo.sectionName); sectionInfo.doctor_tv_sectionIntro.setText(basSectionInfo.sectionIntro); return convertView; } class SectionInfo { ImageView doctor_lv_headPortrait; TextView doctor_tv_doctorName; TextView doctor_tv_sectionName; TextView doctor_tv_sectionIntro; }};
(2)、把BaseAdapter放到ListView中
doctor_lv_list.setAdapter(baseAdapter);
(3)、绑定数据(数据加载完毕,通知列表去更新)
baseAdapter.notifyDataSetChanged();
阅读全文
0 0
- Android Studio ListView列表绑定网络JSON数据
- Android Studio 绑定下拉框数据(网络JSON数据)
- Android 获取XML网络数据并绑定到ListView
- Android:SQLite数据绑定ListView
- Android Studio json 数据
- Android Studio 获取网络JSON数据并处理
- Json解析网络数据+listview展示跳转
- wemall app商城源码Android 获取XML网络数据并绑定到ListView
- android studio解析JSON数据
- android listview与sqlite数据绑定
- android实例 listview与sqlite数据绑定
- android 数组数据绑定到listview
- Android学习笔记--ListView的数据绑定
- android实例 listview与sqlite数据绑定
- android实例 listview与sqlite数据绑定
- Android 实现json网络数据通过BaseAdapter加载到ListView中
- Android--使用原生技术实现ListView(原生技术实现网络异步请求,解析json数据)
- Android studio请求网络数据ListView异步加载展示文字条目 ImageLoadler加载图片
- 报数-LintCode
- EventBus的简单实现与log日志的使用
- 数据结构:实验二线性表综合实践⑤间接寻址
- SVN failed to commit: 'xxx\build\xxx' is scheduled for addition, but is missing
- STM32 CAN使用
- Android Studio ListView列表绑定网络JSON数据
- 各大公司Java后端开发面试题总结
- 悟性高还不行,还得有逆向思维
- 构建工具-Gradle简介-1
- 中国首届原型设计大赛在成都举办
- Hadoop 2.6 使用MapReduce实现基于内容的推荐系统
- 前端几个常用简单的开发手册拿走不谢
- HashMap 遍历
- 图片的三级缓存