新闻Xlistview 包括 无网络缓存

来源:互联网 发布:js默认隐藏div 编辑:程序博客网 时间:2024/05/22 03:19
public class f1 extends Fragment implements XListView.IXListViewListener{    @Nullable    XListView xListView;    String filePath = "/sdcard/Android/data/hjy.x1/Text/";    String fileName = "gsonData.json";    //页码    private int pageIndex = 1;    //默认值是false    private boolean flag;    MyAdapter ma;    List<HotBean.ResultBean.DataBean> listbean_title;    f1sqlManger f1sqlManger;    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {        View view = inflater.inflate(R.layout.f1, container, false);        xListView= (XListView) view.findViewById(R.id.xlv);        //设置是否允许上下拉加载更多数据        xListView.setPullLoadEnable(true);        xListView.setXListViewListener(this);        f1sqlManger=new f1sqlManger(getActivity());        return view;    }    //获得数据    @Override    public void onActivityCreated(@Nullable Bundle savedInstanceState) {        super.onActivityCreated(savedInstanceState);         getweb();        //无网络加载        detect(getActivity());    }    public boolean detect(Activity act) {        ConnectivityManager manager = (ConnectivityManager) act                .getApplicationContext().getSystemService(                        Context.CONNECTIVITY_SERVICE);        if (manager == null) {            return false;        }        NetworkInfo networkinfo = manager.getActiveNetworkInfo();        if (networkinfo == null || !networkinfo.isAvailable()) {            //无网络 显示弹窗            showDialog();            Toast.makeText(getActivity(),"无网络连接",Toast.LENGTH_SHORT).show();            return false;        }        Toast.makeText(getActivity(),"网络连接正常",Toast.LENGTH_SHORT).show();        return true;    }    /**     * 提示用户的一个联网对话框     */    private void showDialog(){        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());        builder.setMessage("无网络需要连接网络");        builder.setNegativeButton("取消并加载本地缓存",new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                //加载本地的数据库文件                listbean_title=f1sqlManger.selest_List();                if(ma == null) {                    ma=new MyAdapter(getActivity(),listbean_title);                    //3.将数据映射到ListViewx                    xListView.setAdapter(ma);                }else{                    //将获取到的新数据添加到原来与adapter绑定集合里面                    ma.loadMore(listbean_title,flag);                }            }        });        builder.setPositiveButton("跳转到网络设置", new DialogInterface.OnClickListener() {            @Override            public void onClick(DialogInterface dialog, int which) {                //设置跳转网络界面                startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));            }        });        builder.create().show();    }    private  void getweb(){        getHttp("http://v.juhe.cn/toutiao/index?type=top&key=bf95c49aa0686717f1fb26bd9bf1772d");    }    private void getHttp(String path) {        new AsyncTask<String,Void,String>(){            @Override            protected void onPostExecute(String s) {                super.onPostExecute(s);                if(s!=null){                    Gson gson=new Gson();                  HotBean hb=  gson.fromJson(s, HotBean.class);                    final List<HotBean.ResultBean.DataBean> listbean=hb.getResult().getData();                    //数据库 存数据                    f1sqlManger.insert(listbean);                                        if(ma == null) {                        ma=new MyAdapter(getActivity(),listbean);                        //3.将数据映射到ListView                        xListView.setAdapter(ma);                    }else{                        //将获取到的新数据添加到原来与adapter绑定集合里面                        ma.loadMore(listbean,flag);                    }                    xListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {                        @Override                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                            HotBean.ResultBean.DataBean hrd= listbean.get(position-1);                            Intent intent = new Intent(getActivity(), WebActivity.class);                            intent.putExtra("url", hrd.getUrl());                            startActivity(intent);                        }                    });                }else {                   return;                }            }            @Override            protected String doInBackground(String... params) {                try {                    String path=params[0];                    URL url=new URL(path);                    HttpURLConnection httpcon= (HttpURLConnection) url.openConnection();                    httpcon.setRequestMethod("GET");                    httpcon.setConnectTimeout(5000);                    httpcon.setReadTimeout(5000);                    int code=httpcon.getResponseCode();                    if(code==200){                        InputStream is=httpcon.getInputStream();                        return StreamTools.read(is);                    }                } catch (IOException e) {                    e.printStackTrace();                }                return null;            }        }.execute(path);    }    /**     * 下拉的时候会被执行     */    @Override    public void onRefresh() {        flag = false;        ++pageIndex;        getweb();        xListView.stopRefresh(true);    }    //上拉    @Override    public void onLoadMore() {        flag = true;        ++pageIndex;        getweb();        //停止加载更多        xListView.stopLoadMore();    }}
原创粉丝点击