[Java] 刷新 →→→→→进入此内容的聊天室

来自 , 2019-07-16, 写在 Java, 查看 116 次.
URL http://www.code666.cn/view/ad71c82b
  1. package com.example.day09_xlistview;
  2.  
  3. import java.text.SimpleDateFormat;
  4. import java.util.ArrayList;
  5. import java.util.Date;
  6. import java.util.List;
  7.  
  8. import com.example.day09_xlistview.bean.Bean;
  9. import com.example.day09_xlistview.bean.Info;
  10. import com.example.day09_xlistview.view.XListView;
  11. import com.example.day09_xlistview.view.XListView.IXListViewListener;
  12. import com.google.gson.Gson;
  13. import com.lidroid.xutils.HttpUtils;
  14. import com.lidroid.xutils.exception.HttpException;
  15. import com.lidroid.xutils.http.ResponseInfo;
  16. import com.lidroid.xutils.http.callback.RequestCallBack;
  17. import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
  18.  
  19. import android.app.Activity;
  20. import android.os.Bundle;
  21. import android.os.Handler;
  22. import android.os.Message;
  23. import android.view.Menu;
  24. import android.view.MenuItem;
  25. import android.view.View;
  26. import android.widget.ArrayAdapter;
  27.  
  28. public class MainActivity extends Activity implements IXListViewListener {
  29.         String[] arr = new String[] { "1", "2", "3", "4", "5",
  30.  
  31.         };
  32.         // String path =
  33.         // "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno="
  34.         // + 1;
  35.  
  36.         int i = 1;
  37.         private XListView xListView;
  38.         List<Info> list = new ArrayList<>();
  39.         private MyBaseAdapter myBaseAdapter;
  40.         Handler handler = new Handler() {
  41.  
  42.                 public void handleMessage(android.os.Message msg) {
  43.  
  44.                         String s = (String) msg.obj;
  45.  
  46.                         Gson gson = new Gson();
  47.                         Bean bean = gson.fromJson(s, Bean.class);
  48.  
  49.                         int tag = msg.arg1;
  50.  
  51.                         switch (tag) {
  52.                         // 下拉刷新
  53.                         case 1:
  54.                                 // 清空之前的数据
  55.                                 list.clear();
  56.                                 // 添加新数据
  57.                                 list.addAll(bean.result.list);
  58.                                 myBaseAdapter.notifyDataSetChanged();
  59.                                 // 刷新适配器
  60.                                 // 停止刷新
  61.                                 xListView.stopRefresh();
  62.                        
  63.                                 //格式化毫秒值
  64.                                 SimpleDateFormat dateFormat=new SimpleDateFormat("hh:mm:ss");
  65.                                
  66.                                 String format = dateFormat.format(new Date());
  67.                                
  68.                                 xListView.setRefreshTime(format);
  69.                                
  70.                                 break;
  71.  
  72.                         // 上拉加载
  73.                         case 2:
  74.                                 // 追加新数据
  75.                                 list.addAll(bean.result.list);
  76.                                 myBaseAdapter.notifyDataSetChanged();
  77.                                 // 停止加载更多
  78.                                 xListView.stopLoadMore();
  79.                                 break;
  80.                         // 加载第一次的数据
  81.                         case 3:
  82.  
  83.                                 list.addAll(bean.result.list);
  84.  
  85.                                 myBaseAdapter = new MyBaseAdapter(list,MainActivity.this);
  86.  
  87.                                 xListView.setAdapter(myBaseAdapter);
  88.  
  89.                                 break;
  90.                         }
  91.  
  92.                 };
  93.  
  94.         };
  95.  
  96.         private ArrayAdapter<String> arrayAdapter;
  97.  
  98.         @Override
  99.         protected void onCreate(Bundle savedInstanceState) {
  100.                 super.onCreate(savedInstanceState);
  101.                 setContentView(R.layout.activity_main);
  102.  
  103.                 xListView = (XListView) findViewById(R.id.xListView);
  104.                 // 开启加载更多
  105.                 xListView.setPullLoadEnable(true);
  106.                 // 设置适配器前就要有数据
  107.                 getData(3);
  108.  
  109.                 // 设置xListView上拉,下拉监听
  110.                 xListView.setXListViewListener(this);
  111.  
  112.         }
  113.  
  114.         @Override
  115.         // xListView下拉刷新监听方法
  116.         public void onRefresh() {
  117.                 // 联网得到新数据
  118.  
  119.                 getData(1);
  120.                
  121.  
  122.         }
  123.  
  124.         @Override
  125.         // xListView上拉加载更多监听方
  126.         public void onLoadMore() {
  127.                 getData(2);
  128.                
  129.         }
  130.  
  131.         /**
  132.          * 请求网络数据
  133.          */
  134.         private void getData(final int tag) {
  135.  
  136.                 String path = null;
  137.                 // 是刷新调用的联网请求
  138.                 if (tag == 1) {
  139.  
  140.                         path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
  141.                         // 是加载更多调用的联网请求
  142.                 } else if (tag == 2) {
  143.  
  144.                         path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno="
  145.                                         + (i += 1);
  146.                         // 加载第一次的数据
  147.                 } else if (tag == 3) {
  148.                         path = "http://v.juhe.cn/weixin/query?key=b3edac6e41f3f3c84b855019e47b7ace&pno=" + 1;
  149.  
  150.                 }
  151.  
  152.                 HttpUtils httpUtils = new HttpUtils();
  153.  
  154.                 httpUtils.send(HttpMethod.GET, path, new RequestCallBack<String>() {
  155.  
  156.                         @Override
  157.                         public void onFailure(HttpException arg0, String arg1) {
  158.  
  159.                         }
  160.  
  161.                         @Override
  162.                         public void onSuccess(ResponseInfo<String> arg0) {
  163.                                 String result = arg0.result;
  164.  
  165.                                 // 把响应信息发送给handler
  166.                                 Message msg = Message.obtain();
  167.  
  168.                                 msg.obj = result;
  169.                                 msg.arg1 = tag;
  170.  
  171.                                 handler.sendMessage(msg);
  172.  
  173.                         }
  174.                 });
  175.         }
  176. }
  177.  

回复 "刷新"

这儿你可以回复上面这条便签

captcha