网络连接取数据,并加载到ListView的实现

对于ListView 可以实现上啦刷新,下拉加载更多只需要实现接口AbsListView.OnScrollListener
    方法
      
    
    public class XMLUseConnectionUtil_Activity extends AppCompatActivity implements AbsListView.OnScrollListener{
    String httpUrl = "http://192.168.5.10/serv-app/around";//使用Tomcat服务器加载数据的地址
    ConnectionUtil connectionUtil;
    List<Book> books=null;
    Book book=null;
    ListView mListView;
    List<Hotel> hotellist = new ArrayList<>();
    JsonAdapter jsonAdapter;

    public static final int PAGE_NUM = 20;//每页数据个数
    public int TotalPageCount=5;//总页数
    public int CurrentPageNum = 1;//当前页号
    public boolean isLastPage = false;//是否滚动到最底部
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xmluse_connection_util_layout);
        mListView = (ListView) findViewById(R.id.xmluse_connection_util_listview);
        connectionUtil = new ConnectionUtil(this);

        doDownLoadTxt();
        jsonAdapter = new JsonAdapter(this);
        mListView.setAdapter(jsonAdapter);
    }
    public void doDownLoadTxt(){
        connectionUtil.asyncConnect(httpUrl,ConnectionUtil.Mothod.GET,ConnectionUtil.Cache.TRUE, new ConnectionUtil.HttpConnectionInterface(){
            public void excute(String message) {
                Logs.e("XMLUseConnectionUtil_Activity.message>>>>"+message);
                if(message == null) {    //通过content==null 是否有网络
                    Toast.makeText(XMLUseConnectionUtil_Activity.this, "连接超时,请检查网络环境!", Toast.LENGTH_SHORT).show();
                }else {
                    doMerchantJsonParase(message);
                    jsonAdapter.SetDatalist(hotellist);
                }
                if(CurrentPageNum == 1) {
                    jsonAdapter.SetDatalist(hotellist);
                }else{
                    jsonAdapter.addDataList(hotellist);
                }
            }
        });
    }


    /**
     * 检查网络连接是否可用
     */
    public boolean isCheckedConnection(){
        ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);//获得系统联网服务
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if(networkInfo != null && networkInfo.isConnected()){
            return true;
        }else {
            return false;
        }
    }
        //检查wifi网络是否可用
    public boolean isWiFiActive(Context inContext) {
        Context context = inContext.getApplicationContext();
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivity != null) {
            NetworkInfo[] info = connectivity.getAllNetworkInfo();
            if (info != null) {
                for (int i = 0; i < info.length; i++) {
                    if (info[i].getTypeName().equals("WIFI") && info[i].isConnected()) {
                        return true;
                    }
                }
            }
        }
        return false;
    }


    public void doMerchantJsonParase(String jsonStr){

        /**
         * 使用Gson工具类,首先引入工具包 file--Project Structure--app--Dependencies--"+"--library dependency(加载在线)--搜索谷歌com.google.decode.Gson就可以了
         *                                                                                  File dependency(加载离线工具包)
         *   按照数据格式{"name":12,"object":{"total":28}}
         *   由里到外依次建立相应的类,如果是数组类型的建立的类 ArrayList<Student> merchantKey 作为成员,而Student类就负责具体的数组所包含的项
         */
        Gson gson = new Gson();

        Gsons gsons = gson.fromJson(jsonStr,Gsons.class);
        Logs.e("111"+jsonStr);
        Info info = gsons.getInfo();
        ArrayList<Student> list = info.getMerchantKey();
        int length = list.size();
        Logs.e("length>>>>"+length);
        for(Student student:list){
            Hotel hotel = new Hotel();//必须写在里面,这样每遍历一次,实例化一次,hotellist加载才会得到不同的view,定义在外面,加载的都相同,并且是最后一项
            com.example.scxh.myapp.Logs.e(student.getId()+" "+student.getName()+" "+student.getCoupon()+" "+student.getCardType()+" "+student.getLocation()+" "+student.getDistance()
                    +" "+student.getPicUrl()+" "+student.getCouponType()+" "+student.getCardType()+" "+student.getGroupType()+" "
                    +student.getGpsX()+" "+student.getGpsY()+" "+student.getGoodSayNum()+" "+student.getMidSayNum()+" "+student.getBasSayNum());
            hotel.setName(student.getName());
            hotel.setCoupon(student.getCoupon());
            hotel.setLocation(student.getLocation());
            hotel.setDistance(student.getDistance());
            hotel.setPicUrl(student.getPicUrl());
            hotel.setCouponType(student.getCouponType());
            hotel.setCardType(student.getCardType());
            hotel.setGroupType(student.getGroupType());
            hotel.setGpsX(student.getGpsX());
            hotel.setGpsY(student.getGpsY());
            hotel.setGoodSayNum(student.getGoodSayNum());
            hotel.setMidSayNum(student.getMidSayNum());
            hotel.setBasSayNum(student.getBasSayNum());
            hotellist.add(hotel);
        }

    }

    @Override
    public void onScrollStateChanged(AbsListView absListView, int scrollState) {
        Logs.e("onScrollStateChanged.scrollState"+scrollState);
        if(scrollState == SCROLL_STATE_IDLE && isLastPage){
            isLastPage = false;
            if(++ CurrentPageNum > TotalPageCount){
                Toast.makeText(this,"已加载到最后一页",Toast.LENGTH_SHORT).show();
                CurrentPageNum = TotalPageCount;
            }else {
                doDownLoadTxt();
            }
        }


    }
    /**
     * 滚动时一直回调,直到停止滚动时才停止回调。单击时回调一次。
     * firstVisibleItem:当前能看见的第一个列表项ID(从0开始)
     * visibleItemCount:当前能看见的列表项个数(小半个也算)
     * totalItemCount:列表项共数
     */
    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        Logs.e("firstVisibleItem :" + firstVisibleItem + " ,visibleItemCount :" + visibleItemCount + " ,totalItemCount :" + totalItemCount);
        if ((firstVisibleItem + visibleItemCount) == totalItemCount) {
            isLastPage = true;
        }
    }


    class JsonAdapter extends BaseAdapter {
        List<Hotel> list = new ArrayList<>();
        LayoutInflater layoutInflater;
        public JsonAdapter(Context context){
            this.layoutInflater = LayoutInflater.from(context);
        }
        public void SetDatalist(List<Hotel> list){
            this.list = list;
            notifyDataSetChanged();
        }
        public void addDataList(List<Hotel> list){
            this.list.addAll(list);
            notifyDataSetChanged();
        }
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

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

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            HoldView holdView;
            if(view == null){
                view = layoutInflater.inflate(R.layout.activity_jsonlistview_layout,null);
                ImageView imageView = (ImageView) view.findViewById(R.id.json_listview_img);
                ImageView card = (ImageView) view.findViewById(R.id.json_card);
                ImageView group = (ImageView) view.findViewById(R.id.json_ticket);
                ImageView tuan = (ImageView) view.findViewById(R.id.json_tuan);
                TextView hotel = (TextView) view.findViewById(R.id.json_hotel);
                TextView youhui = (TextView) view.findViewById(R.id.json_youhui);
                TextView location = (TextView) view.findViewById(R.id.json_location);
                TextView distance = (TextView) view.findViewById(R.id.json_distance);

                holdView = new HoldView();
                holdView.pic = imageView;
                holdView.card = card;
                holdView.group = group;
                holdView.tuan = tuan;
                holdView.hotel = hotel;
                holdView.youhui = youhui;
                holdView.location = location;
                holdView.distance = distance;
                view.setTag(holdView);
            }
            holdView = (HoldView) view.getTag();
            Hotel hotel = (Hotel) getItem(i);
            String name= hotel.getName();
            String coupon= hotel.getCoupon();
            String location= hotel.getLocation();
            String distance= hotel.getDistance();
            String picUrl= hotel.getPicUrl();
            holdView.hotel.setText(name);
            holdView.youhui.setText(coupon);
            holdView.location.setText(location);
            holdView.distance.setText(distance);
            String couponType= hotel.getCouponType();
            String cardType= hotel.getCardType();
            String groupType= hotel.getGroupType();
            holdView.card.setImageBitmap(cardType.equals("YES")? BitmapFactory.decodeResource(getResources(),R.drawable.near_card):null);//根据返回的数据“YES”或者“NO”,使用三目运算;
            holdView.group.setImageBitmap(groupType.equals("YES")? BitmapFactory.decodeResource(getResources(),R.drawable.near_group):null);
            holdView.tuan.setImageBitmap(couponType.equals("YES")? BitmapFactory.decodeResource(getResources(),R.drawable.near_ticket):null);
            Glide.with(XMLUseConnectionUtil_Activity.this).load(picUrl).into(holdView.pic);//对于直接从网络取数据的图片使用第三方包,还可以缓存

            return view;
        }
    }
    public class HoldView{
        ImageView pic;
        ImageView card;
        ImageView group;
        ImageView tuan;
        TextView hotel;
        TextView youhui;
        TextView location;
        TextView distance;
    }
    class Hotel {
        int id;
        String name;
        String coupon;
        String location;
        String distance;
        String picUrl;
        String couponType;
        String cardType;
        String groupType;
        String gpsX;
        String gpsY;
        int goodSayNum;
        int midSayNum;
        int badSayNum;
        public int getId() {
            return id;
        }

        public void setId(int id) {
            this.id = id;
        }

        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 int getGoodSayNum() {
            return goodSayNum;
        }

        public void setGoodSayNum(int goodSayNum) {
            this.goodSayNum = goodSayNum;
        }

        public int getMidSayNum() {
            return midSayNum;
        }

        public void setMidSayNum(int midSayNum) {
            this.midSayNum = midSayNum;
        }

        public int getBasSayNum() {
            return badSayNum;
        }

        public void setBasSayNum(int badSayNum) {
            this.badSayNum = badSayNum;
        }


    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值