上图是一个Gridview列表:但是后台给的就看就是这样的接口。这是我挑的重点部分,有需要完整代码的可以联系我
这是接口数据:
"carArchive": {
"排放量": "2.3",
"表显里程": "2.0万公里",
"看车地点": "安次区",
"变速箱": "自动档",
"过户次数": "1次过户",
"排放标准": "国三",
"来源": "商家",
"车牌所在地": "廊坊",
"2013-05上牌": "5年"
}
Bean类:
public static class BeanCarArchiveJson {
private String carKey;
private String carValue;
public String getCarKey() {
return carKey;
}
public void setCarKey(String carKey) {
this.carKey = carKey;
}
public String getCarValue() {
return carValue;
}
public void setCarValue(String carValue) {
this.carValue = carValue;
}
}
Activity:
private List
//接口数据----其中一部分是我listview的数据,可以忽略
private void reqData() {
Intent intent = getIntent();
int ucid = intent.getIntExtra("ucid", 0);
LogUtil.error("ucid2-->", ucid);
String path = HostStudentNew.CAR_DETAILS + "?ucid=" + ucid;
DefaultStringRequestNew req = new DefaultStringRequestNew(Request.Method.GET, path, new ResponseListener() {
@Override
public void onSuccess(String response) {
try { //解析Gridview数据
JSONObject jsonObject = new JSONObject(response);
JSONObject object = jsonObject.getJSONObject("data");
JSONObject ob = object.getJSONObject("carArchive");
Iterator
list1gridview = new ArrayList();
while (iterator.hasNext()) {
key = iterator.next();
value = ob.get(key).toString();
Log.e("key-value", "key:" + key + " value:" + value);
BeanCarDetails.BeanCarArchiveJson archive = new BeanCarDetails.BeanCarArchiveJson();
archive.setCarKey(key);
archive.setCarValue(value);
list1gridview.add(archive);
}
adaptergv.setDataList(list1gridview);
} catch (JSONException e) {
e.printStackTrace();
}
//解析其余详情数据
BeanCarDetails detailsBean = JsonUtils.fromJson(response, BeanCarDetails.class);
if (detailsBean != null && detailsBean.getData() != null) {
BeanCarDetails.BeanCarEntity data = detailsBean.getData();
usedCar = data.getUsedCar();
listimg = data.getImgList();//加载Banner轮播图
if (listimg != null && listimg.size() > 0) {
initBanner();
}
mTvcarnames.setText(usedCar.getShowName());//汽车标题
mTvcarprices.setText(usedCar.getPrice() + "");//二手车价格
mTvbriefIntro.setText(usedCar.getBriefIntro());//汽车公里数
mTvnewprice.setText(usedCar.getMarketPrice()+"");//获取新车价格
mTvshopname.setText(usedCar.getName());//商铺名
mTvshopaddress.setText(usedCar.getAddress());//商铺地址
telephone = usedCar.getTelephone();//商铺电话
list1listview = data.getAttrList();//listview集合添加数据
adapterlv.setDataList(list1listview);
}
}
}, new DefaultErrorListener());
VolleyUtil.getInstance().addToRequestQueue(req);
}
//适配器
private void initAdapter() {
// 车辆档案
adaptergv = new MyCarAdaptergv(mContext, list1gridview);
mGvcarList.setAdapter(adaptergv);
// 车辆配置
adapterlv = new MyCarAdapterlv(mContext, list1listview);
mLvcarlist.setAdapter(adapterlv);
}
//这是我的适配器:
public class MyCarAdaptergv extends AbstractAdapter
public MyCarAdaptergv(Context mContext, List
super(mContext, dataList);
}
@Override
public int getListViewItemId() {
return R.layout.item_car_gv_list;
}
@Override
public void convert(int position, View convertView, BeanCarDetails.BeanCarArchiveJson dataItem) {
TextView filekilometers = convertView.findViewById(R.id.tv_file_kilometers);
TextView filemileage = convertView.findViewById(R.id.tv_file_mileage);
filekilometers.setText(dataItem.getCarKey());
filemileage.setText(dataItem.getCarValue());
}
}
因为继承了父类,我把父类也贴出来:
/**
* 封装的单个布局的Adapter
*
* @author gaobingbing
*/
public abstract class AbstractAdapter
public AbstractAdapter(Context mContext,List
super(mContext,dataList);
}
public abstract int getListViewItemId();
@Override
public View getView(int position,View convertView,ViewGroup parent){
if(convertView == null) {
convertView = mInflater.inflate(getListViewItemId(),parent,false);
}
convert(position,convertView,getItem(position));
return convertView;
}
public abstract void convert(int position,View convertView,T dataItem);
}
这就是我的完整解析流程。