1、创建xml页面文件(ListView数据列表载体)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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数据绑定载体
<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>
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();