Json解析后添加到ListView控件上

来源:互联网 发布:淘宝2016创业成功案例 编辑:程序博客网 时间:2024/06/06 07:13

主要实现的功能:就是将Jsonj解析后,显示在ListV iew控件上(包括网络取图片,是否显示劵、卡,团)

Json

{"resultCode":1,"resultInfo":"SUCCESS","info":{"pageInfo":{"total":28,"pageSize":10,"lastPageNumber":3,"nowPage":1,"currNum":10},"merchantKey":[{"merchantID":"5327","name":"瑞庭竹岛酒店","coupon":"网上预定入住可享返现优惠","location":"四川省成都市高新区老成仁路8号","distance":"203m","picUrl":"http://www.warmtel.com/igme_pic/fe3e79c1f349474b98f06477bbdc009f.jpg","couponType":"YES","cardType":"NO","groupType":"NO","gpsX":104.079935,"gpsY":30.54066,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"2845","name":"目咖啡软件园C6店","coupon":"凭QQ美食享咖啡8折优惠","location":"四川省成都市高新区天华二路219号天府软件园C区6号楼1楼","distance":"479m","picUrl":"http://www.warmtel.com/igme_pic/33f7ab6e385143f097527d4507cabcbe.jpg","couponType":"YES","cardType":"NO","groupType":"NO","gpsX":104.078242,"gpsY":30.545434,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"5326","name":"九点国际酒店","coupon":"网上预定成功入住可返现金","location":"四川省成都市天府大道南段1号齐盛艺境[近世纪城会展中心]","distance":"556m","picUrl":"http://www.warmtel.com/igme_pic/c5721d159e63482b8a1d4dd9f70f9a03.jpg","couponType":"YES","cardType":"NO","groupType":"NO","gpsX":104.077416,"gpsY":30.53835,"goodSayNum":1,"midSayNum":0,"badSayNum":0},{"merchantID":"2841","name":"布衣客栈软件园店","coupon":"酒店消费券预订酒店返现金","location":"四川省成都市高新区世纪城南路399号[近天府软件园C区]","distance":"660m","picUrl":"http://www.warmtel.com/igme_pic/f8dc2d69a7b742eba4408bb72e510f5d.jpg","couponType":"YES","cardType":"YES","groupType":"NO","gpsX":104.080515,"gpsY":30.547937,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"11798","name":"茅台贵州液专卖店","coupon":"购买茅台红酒3件以上再送1件","location":"四川省成都市高新区中和镇姐儿堰路14号","distance":"683m","picUrl":"http://www.warmtel.com/igme_pic/df0a55009cea4b8ba2773278ffb3ff79.jpg","couponType":"NO","cardType":"NO","groupType":"NO","gpsX":104.079227,"gpsY":30.536175,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"2842","name":"巴国布衣软件园店","coupon":"持光大银行信用卡享成都巴国布衣8.8折优惠,截止2012-12-31.","location":"四川省成都市高新区世纪城南路399号","distance":"683m","picUrl":"http://www.warmtel.com/igme_pic/1f4781f9329b40218544fd4b7ec673c9.jpg","couponType":"NO","cardType":"YES","groupType":"NO","gpsX":104.080254,"gpsY":30.548117,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"5325","name":"香妃儿美容美体","coupon":"38元享980元超值美胸丰胸套餐","location":"四川省成都市高新区远大都市风景二期二号","distance":"703m","picUrl":"http://www.warmtel.com/igme_pic/26cfa2c850dd40cf9518d71ea55b687b.jpg","couponType":"NO","cardType":"NO","groupType":"NO","gpsX":104.080344,"gpsY":30.535779,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"3718","name":"锦咖啡","coupon":"凭锦咖啡折扣券可享餐品8.5折,饮品7.5折,牛排6.8折优惠.\r\n\r\n1.该优惠有效期:截止至2012年12月31日;\r\n2.凭此券可以享受以下优惠:\r\n餐品8.5折\r\n饮品7.5折\r\n牛排6.8折\r\n(营业时间:09:30-23:30)\r\n\r\n展示此页即享优惠.","location":"四川省成都市高新区天华路447-1号[近天府软件园B区]","distance":"874m","picUrl":"http://www.warmtel.com/igme_pic/7629ff4f8beb43d1bd0967baba46f6c6.jpg","couponType":"YES","cardType":"YES","groupType":"NO","gpsX":104.078756,"gpsY":30.549577,"goodSayNum":3,"midSayNum":0,"badSayNum":0},{"merchantID":"2416","name":"蜀国飘香远大店","coupon":"享菜品8.8折优惠(酒水除外)","location":"四川省成都市高新区远大都市风景商业街3号楼","distance":"1041m","picUrl":"http://www.warmtel.com/igme_pic/8f0e793995084f76a59cc789fb6f6c7e.jpg","couponType":"NO","cardType":"NO","groupType":"NO","gpsX":104.077334,"gpsY":30.533345,"goodSayNum":0,"midSayNum":0,"badSayNum":0},{"merchantID":"3719","name":"华庭茶府","coupon":"100元享120元小包优惠","location":"四川省成都市高新区天华路329号附1号[近天府软件园B区]","distance":"1057m","picUrl":"http://www.warmtel.com/igme_pic/1eeae0d361f54124bdd3b6600dcc6074.jpg","couponType":"YES","cardType":"YES","groupType":"NO","gpsX":104.078705,"gpsY":30.551274,"goodSayNum":0,"midSayNum":0,"badSayNum":0}]}}
这里看起来是比较乱的,所以要用Json工具视图打开,就会一层层的显示出来:

JSONresultCode : 1resultInfo : "SUCCESS"infopageInfomerchantKey0merchantID : "5327"name : "瑞庭竹岛酒店"coupon : "网上预定入住可享返现优惠"location : "四川省成都市高新区老成仁路8号"distance : "203m"picUrl : "http://www.warmtel.com/igme_pic/fe3e79c1f349474b98f06477bbdc009f.jpg"couponType : "YES"cardType : "NO"groupType : "NO"gpsX : 104.079935gpsY : 30.54066goodSayNum : 0midSayNum : 0badSayNum : 0123456789
------------------------------------------------------------------------------------------------------
接下来就看看用代码实现其功能,首先从Json最里面一层一层外面写:

第一步:

package com.example.jsonlistview;


public class Totel {
private String merchantID;
private String name;
private String coupon;
private String location;
private String distance;
private String picUrl;
private String couponType;
private String cardType;
private String groupType;
private String gpsX;
private String gpsY;
private String goodSayNum;
private String midSayNum;
private String badSayNum;
public String getMerchantID() {
return merchantID;
}
public void setMerchantID(String merchantID) {
this.merchantID = merchantID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCoupon() {
return coupon;
}
public void setCoupon(String coupon) {
this.coupon = coupon;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getCouponType() {
return couponType;
}
public void setCouponType(String couponType) {
this.couponType = couponType;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getGroupType() {
return groupType;
}
public void setGroupType(String groupType) {
this.groupType = groupType;
}
public String getGpsX() {
return gpsX;
}
public void setGpsX(String gpsX) {
this.gpsX = gpsX;
}
public String getGpsY() {
return gpsY;
}
public void setGpsY(String gpsY) {
this.gpsY = gpsY;
}
public String getGoodSayNum() {
return goodSayNum;
}
public void setGoodSayNum(String goodSayNum) {
this.goodSayNum = goodSayNum;
}
public String getMidSayNum() {
return midSayNum;
}
public void setMidSayNum(String midSayNum) {
this.midSayNum = midSayNum;
}
public String getBadSayNum() {
return badSayNum;
}
public void setBadSayNum(String badSayNum) {
this.badSayNum = badSayNum;
}

}

---------------------------------------------------------------------------------------------------------------

第二步:

package com.example.jsonlistview;


public class Pageinfo {


private int total;
private int pageSize;
private int lastPageNumber;
private int nowPage;
private int currNum;


public int getTotal() {
return total;
}


public void setTotal(int total) {
this.total = total;
}


public int getPageSize() {
return pageSize;
}


public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}


public int getLastPageNumber() {
return lastPageNumber;
}


public void setLastPageNumber(int lastPageNumber) {
this.lastPageNumber = lastPageNumber;
}


public int getNowPage() {
return nowPage;
}


public void setNowPage(int nowPage) {
this.nowPage = nowPage;
}


public int getCurrNum() {
return currNum;
}


public void setCurrNum(int currNum) {
this.currNum = currNum;
}
}

--------------------------------------------------------------------------------------------------------------

第三步:

package com.example.jsonlistview;


public class Json {
private String resultCode;
private String resultInfo;
private InFo info;
public String getResultCode() {
return resultCode;
}
public void setResultCode(String resultCode) {
this.resultCode = resultCode;
}
public String getResultInfo() {
return resultInfo;
}
public void setResultInfo(String resultInfo) {
this.resultInfo = resultInfo;
}
public InFo getInfo() {
return info;
}
public void setInfo(InFo info) {
this.info = info;
}
}
---------------------------------------------------------------------------------------------------------------

第四步:开始写适配器

package com.example.jsonlistview;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class Myadapters extends BaseAdapter {
private List<UserBean> mList = new ArrayList<UserBean>();
private LayoutInflater mInflater;
private Context mcontext;


public Myadapters(Context context) {
mcontext = context;
mInflater = LayoutInflater.from(mcontext);


}
public void setData(List<UserBean> list) {
this.mList = list;
notifyDataSetChanged();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();


}


@Override
public Object getItem(int position) {
return mList.get(position);
}


@Override
public long getItemId(int position) {
return position;
}


public View getView1(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return null;
}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
Log.e("aa", "dasdasda");
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.listvtxtv, null);
holder.titleTxt = (TextView) convertView
.findViewById(R.id.listtex1);
holder.neirongTxt = (TextView) convertView
.findViewById(R.id.listtex2);
holder.infoTxt = (TextView) convertView.findViewById(R.id.listtex3);
holder.infoTxt1 = (TextView) convertView
.findViewById(R.id.listtex31);
holder.imageView = (ImageView) convertView.findViewById(R.id.imgs);
convertView.setTag(holder);
holder.imageViewj = (ImageView) convertView.findViewById(R.id.imgjj);
convertView.setTag(holder);
holder.imageViewtu = (ImageView) convertView.findViewById(R.id.imgtuan);
convertView.setTag(holder);
holder.imageViewka = (ImageView) convertView.findViewById(R.id.imgka);
convertView.setTag(holder);

} else {
holder = (ViewHolder) convertView.getTag();
}
//判斷劵卡团图案片是否显示
String jj=mList.get(position).getGroupType();
Log.e("jj", jj+"");
if(jj.equals("YES")){
holder.imageViewj.setVisibility(View.VISIBLE);
}
else{
holder.imageViewj.setVisibility(View.GONE);
}
String tuan=mList.get(position).getCardType();
if(tuan.equals("YES")){

holder.imageViewtu.setVisibility(View.VISIBLE);
}
else{
holder.imageViewtu.setVisibility(View.GONE);
}
String kas=mList.get(position).getCouponType();
if(kas.equals("YES")){

holder.imageViewka.setVisibility(View.VISIBLE);
}
else{
holder.imageViewka.setVisibility(View.GONE);
}


holder.neirongTxt.setText(mList.get(position).getName());
holder.titleTxt.setText(mList.get(position).getCoupon());
holder.infoTxt.setText(mList.get(position).getDistance());
holder.infoTxt1.setText(mList.get(position).getLocation());
//用Picasso进行网络取图片
Picasso.with(mcontext).load(mList.get(position).getPicUrl())
.into(holder.imageView);
return convertView;
}


class ViewHolder {
ImageView imageViewka=null;
ImageView imageViewtu=null;
ImageView imageViewj=null;
ImageView imageView = null;
TextView titleTxt = null;
TextView neirongTxt = null;
TextView infoTxt = null;
TextView infoTxt1 = null;
}
}

--------------------------------------------------------------------------------------------------------------

第五步:第五步把json里边需要显示的属性建一个类写出来

package com.example.jsonlistview;


public class UserBean {
private String name;
private String coupon;
private String location;
private String distance;
private String picUrl;

private String couponType;
private String cardType;
private String groupType;

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCoupon() {
return coupon;
}
public void setCoupon(String coupon) {
this.coupon = coupon;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getDistance() {
return distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getCouponType() {
return couponType;
}
public void setCouponType(String couponType) {
this.couponType = couponType;
}
public String getCardType() {
return cardType;
}
public void setCardType(String cardType) {
this.cardType = cardType;
}
public String getGroupType() {
return groupType;
}
public void setGroupType(String groupType) {
this.groupType = groupType;
}         
}
-------------------------------------------------------------------------------------------------------------------------------

第六步:第六步在主Activyi里面写异步(网络取数据)及添加json属性

package com.example.jsonlistview;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;


import com.google.gson.Gson;


import android.app.Activity;
import android.database.MergeCursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.widget.ListView;


public class JsonActivity extends Activity {
String http = "http://192.168.1.127:8080/json/around";


private ListView mListView;
private Myadapters myadapters;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_json);


mListView = (ListView) findViewById(R.id.totellistview);
myadapters = new Myadapters(this);
mListView.setAdapter(myadapters);
new AsyncTask<String, Void, List<UserBean>>() {


@Override
protected List<UserBean> doInBackground(String... params) {
// TODO Auto-generated method stub
return getData();
}


@Override
protected void onPostExecute(List<UserBean> result) {
// TODO Auto-generated method stub
Log.e("result", result + "");
myadapters.setData(result);
}
}.execute();


}


private List<UserBean> getData() {
URL url = null;
InputStream is = null;
List<UserBean> mlist = new ArrayList<UserBean>();
StringBuffer sb = new StringBuffer();
BufferedReader br = null;
try {
url = new URL(http);
is = url.openStream();
br = new BufferedReader(new InputStreamReader(is));
Log.e("br", br+"");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Gson gson = new Gson();
Log.e("sb.", sb.toString());
Json json = gson.fromJson(sb.toString(), Json.class);
Log.e("json", json.getResultCode());
List<Totel> totel = json.getInfo().getMerchantKey();
for (Totel t : totel) {
UserBean userbean = new UserBean();
userbean.setName(t.getName());
userbean.setCoupon(t.getCoupon());
userbean.setLocation(t.getLocation());
userbean.setDistance(t.getDistance());
userbean.setPicUrl(t.getPicUrl());
userbean.setCardType(t.getCardType());
userbean.setCouponType(t.getCouponType());
userbean.setGroupType(t.getGroupType());
mlist.add(userbean);
}


return mlist;
}
}

-------------------------------------------------------------------

所有的类就写完了,下面把用到的XML补上

第一个是布局界面和listview

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <LinearLayout        android:layout_width="match_parent"        android:layout_height="40dp"        >        <ImageView            android:layout_marginTop="5dp"            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_marginLeft="5dp"            android:src="@drawable/btn_back" />        <TextView            android:layout_width="wrap_content"            android:layout_height="match_parent"            android:layout_margin="8dp"            android:layout_marginLeft="5dp"            android:text="我的关注"            android:textColor="@android:color/black"            android:textSize="18dp" />    </LinearLayout>    <ListView        android:id="@+id/totellistview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="40dp"        android:layout_marginLeft="10dp" /></RelativeLayout>

第二个Xml就是Myadapters用到的,主要就是在listview里面的内容布局以及控件。

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


    <ImageView
        android:id="@+id/imgs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:src="@drawable/a" />
    


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
<RelativeLayout  
    android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/listtex1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:singleLine="true"
            android:text="fmsdpgfeggf"
           android:maxEms="8"
            android:textColor="@android:color/black" />
      
  <ImageView
        android:id="@+id/imgjj"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_toRightOf="@id/listtex1" 
        android:src="@drawable/tuan_group"
       
        />
      
  <ImageView
        android:id="@+id/imgtuan"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:src="@drawable/juan"
        android:layout_toRightOf="@id/imgjj"
       
        />
      
  <ImageView
        android:id="@+id/imgka"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:src="@drawable/near_card"
        android:layout_toRightOf="@id/imgtuan"
       />
  </RelativeLayout>
        <TextView
            android:id="@+id/listtex2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:text="行的,那是否可"
            android:textColor="@android:color/holo_red_dark" />


        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >


            <TextView
                android:id="@+id/listtex31"
                android:layout_width="150dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="35dp"
                android:ellipsize="end"
                android:singleLine="true"
                android:text="行的,那是否可以设置它左右滚动,不要向下换行"
                android:textSize="12dp" />
<ImageView 
    android:id="@+id/imgviews"
    android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/info_map"
                 android:layout_marginTop="38dp"
               android:layout_toRightOf="@id/listtex31"/>
            <TextView
                android:id="@+id/listtex3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="8dp"
                android:layout_marginTop="35dp"
                android:layout_toRightOf="@id/imgviews"
                android:textSize="12dp" />
                
        </RelativeLayout>
    </LinearLayout>


</LinearLayout>

--------------------------------------------------------------------



0 0
原创粉丝点击