xlistview刷新 加载方法

        需要导入lib

          以下是部分代码



   

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Toast;

import com.bawei.xlistviewlibrary.XListView;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.ex.HttpException;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import adapter.MyAdapter;
import bean.MyBean;
import utils.Myurl;
public class MainActivity extends AppCompatActivity {
    private String path = Environment.getExternalStorageDirectory()+ File.separator;
    private XListView mXLV;
    private List<MyBean.AppBean> mAppBeanList;
    private MyAdapter mAdapter;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化控件
        initview();
        //初始化数据
        initdata();
        mHandler = new Handler();
    }
    private void initview() {
        //找控件
        mXLV = (XListView) findViewById(R.id.xlistview);
        mXLV.setPullRefreshEnable(true);//允许下拉刷新
        mXLV.setPullLoadEnable(true);//允许上啦加载
         mXLV.setXListViewListener(new XListView.IXListViewListener() {
             @Override
             public void onRefresh() {
              mHandler.postDelayed(new Runnable() {
                  @Override
                  public void run() {
                      Toast.makeText(MainActivity.this, "下拉刷新", Toast.LENGTH_SHORT).show();
                     mAppBeanList.clear();
                      initdata();
                      mAdapter.notifyDataSetChanged();
                      onLoad();
                  }
              },2000);
             }

             @Override
             public void onLoadMore() {
               mHandler.postDelayed(new Runnable() {
                   @Override
                   public void run() {
                       Toast.makeText(MainActivity.this, "上拉加载", Toast.LENGTH_SHORT).show();
                       initdata();
                       mAdapter.notifyDataSetChanged();
                       onLoad();
                   }
               },2000);
             }
         });
    }
    private void onLoad() {
        mXLV.stopRefresh();
        mXLV.stopLoadMore();
        //得到系统时间
        mXLV.setRefreshTime(new SimpleDateFormat("hh:mm:ss").
                format(new Date(System.currentTimeMillis())));
    }
    //初始化数据
    private void initdata() {
        RequestParams params = new RequestParams(Myurl.URL);
        x.http().get(params, new Callback.CommonCallback<String>() {



            @Override
            public void onSuccess(String result) {
                String str = result.substring(0, result.length() - 1);
                //解析数据
                Gson gson = new Gson();
                MyBean json = gson.fromJson(str, MyBean.class);
                //得到数据集合
                mAppBeanList = json.getApp();
                //xlistview设置适配器
                mAdapter = new MyAdapter(mAppBeanList,MainActivity.this);
                mXLV.setAdapter(mAdapter);
                //xlistview设置条目点击监听
                mXLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                        Toast.makeText(MainActivity.this,"点击了",Toast.LENGTH_LONG).show();
                        //写一个Dialog
                        new AlertDialog.Builder(MainActivity.this)
                        .setTitle("网络选择")
                        .setIcon(R.mipmap.ic_launcher)
                                //创建对话框
                        .setSingleChoiceItems(new String[]{"wifi", "手机流量"}, 0, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                               switch (which){
                                   case 0:
                                       new AlertDialog.Builder(MainActivity.this)
                                               .setTitle("版本更新")
                                               .setMessage("现在检查到新版本,是否更新?")
                                               .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                                   @Override
                                                   public void onClick(DialogInterface dialog, int which) {
                                                       dialog.dismiss();
                                                   }
                                               })
                                               .setPositiveButton("确定", new DialogInterface.OnClickListener() {

                                                   private ProgressDialog mProgressdialog;

                                                   @Override
                                                   public void onClick(DialogInterface dialog, int which) {
                                                       mProgressdialog = new ProgressDialog(MainActivity.this);
                                                       RequestParams params = new RequestParams(mAppBeanList.get(position).getUrl());
                                                       params.setSaveFilePath(path+mAppBeanList.get(position).getName()+".apk");
                                                       x.http().post(params, new ProgressCallback<File>() {
                                                           @Override
                                                           public void onSuccess(File result) {
                                                               Toast.makeText(MainActivity.this,"下载成功",Toast.LENGTH_SHORT).show();
                                                               Intent intent = new Intent(Intent.ACTION_VIEW);
                                                               intent.setDataAndType(Uri.fromFile(result),"application/vnd.android.package-archive");
                                                               startActivity(intent);
                                                           }

                                                           @Override
                                                           public void onError(Throwable ex, boolean isOnCallback) {
                                                               Toast.makeText(MainActivity.this,"下载失败"+ex.getMessage(),Toast.LENGTH_SHORT).show();
                                                               Log.i("rjz",ex.getMessage());
                                                               if (ex instanceof HttpException) { //网络错误
                                                                   HttpException httpEx = (HttpException) ex;
                                                                   int responseCode = httpEx.getCode();
                                                                   String responseMsg = httpEx.getMessage();
                                                                   String errorResult = httpEx.getResult();

                                                               } else {

                                                               }
                                                           }

                                                           @Override
                                                           public void onCancelled(CancelledException cex) {

                                                           }

                                                           @Override
                                                           public void onFinished() {

                                                           }

                                                           @Override
                                                           public void onWaiting() {

                                                           }

                                                           @Override
                                                           public void onStarted() {

                                                           }

                                                           @Override
                                                           public void onLoading(long total, long current, boolean isDownloading) {
                                                               mProgressdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                                                               mProgressdialog.setMessage("下载中");
                                                               mProgressdialog.show();
                                                               mProgressdialog.setMax((int) total);
                                                               mProgressdialog.setProgress((int) current);
                                                           }
                                                       });

                                                   }
                                               }).show();

                                       break;
                                   case 1:
                                       Intent intent = new Intent(Settings.ACTION_WIFI_SETTINGS);
                                       startActivity(intent);
                                       break;
                               }
                            }
                        }).show();

                    }
                });
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值