xlistview

package com.example.myapplication4;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.xlistviewlib.XListView;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener{

            private String Api="http://api.expoon.com/AppNews/getNewsList/type/1/p/";
            private int num=1;
            private List<Bean.DataBean> list=new ArrayList<>();
            private XListView xListView;
            private MyAdapter myAdapter;
            private Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xListView = findViewById(R.id.xlistview);
        handler=new Handler();
        initData(1);
        myAdapter = new MyAdapter(MainActivity.this,list);
        xListView.setAdapter(myAdapter);
        xListView.setPullLoadEnable(true);//上拉刷新
        xListView.setPullRefreshEnable(true);//下拉刷新(可以不设)
        xListView.setXListViewListener(this);

    }

    @SuppressLint("StaticFieldLeak")
    private void initData(int num) {

        new AsyncTask<String, Void, List<Bean.DataBean>>() {
            @Override
            protected List<Bean.DataBean> doInBackground(String... strings) {
                try {
                     URL url = new URL(strings[0]);
                    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                    urlConnection.setRequestMethod("GET");
                    urlConnection.setReadTimeout(5000);
                    int responseCode = urlConnection.getResponseCode();
                    if (responseCode==200){

                        String string = get2String(urlConnection.getInputStream());
                        final Gson gson = new Gson();
                        final Bean bean = gson.fromJson(string, Bean.class);
                        return bean.getData();
                    }
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(List<Bean.DataBean> dataBeans) {
                super.onPostExecute(dataBeans);
                list.addAll(dataBeans);
                myAdapter.notifyDataSetChanged();
            }
        }.execute(Api+num);



    }
//停止刷新  获得
    private void onLoad() {
        xListView.stopRefresh();//停止刷新
        xListView.stopLoadMore();//停止加载更多
        SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");//设置日期显示格式
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String str = formatter.format(curDate);// 将时间装换为设置好的格式
        xListView.setRefreshTime(str);//设置时间
    }

    private String get2String(InputStream inputStream) throws IOException {
      StringBuilder stringBuilder = new StringBuilder();
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        for (String tem=reader.readLine();tem!=null;tem=reader.readLine()){

            stringBuilder.append(tem);
        }
        return stringBuilder.toString();

    }

    @Override
    public void onRefresh() {
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            num=1;
            list.clear();
            initData(1);
            onLoad();
        }
    },5000);
        myAdapter.notifyDataSetChanged();


    }

    @Override
    public void onLoadMore() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                num++;
                initData(num);
                onLoad();
            }
        },5000);
        myAdapter.notifyDataSetChanged();


    }
}

XlI

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值