Android+Listview+分页+动态加载网络数据

list_item.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.               android:layout_width="fill_parent"  
  4.               android:layout_height="fill_parent"  
  5.               android:orientation="horizontal">  
  6.             
  7. <LinearLayout  
  8.   xmlns:android="http://schemas.android.com/apk/res/android"  
  9.   android:layout_width="fill_parent"  
  10.   android:layout_height="fill_parent"  
  11.   android:orientation="vertical">  
  12.   <TextView  
  13.      android:id="@+id/Gds_Name"  
  14.      android:layout_width="fill_parent"  
  15.      android:layout_height="wrap_content"  
  16.      android:textSize="20px"  
  17.      />  
  18.       
  19.   <TextView  
  20.      android:id="@+id/Gds_Code"  
  21.      android:layout_width="fill_parent"  
  22.      android:layout_height="wrap_content"/>  
  23. </LinearLayout>  
  24. </LinearLayout>  

显示加载更多的布局代码loadmore.xml如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.   xmlns:android="http://schemas.android.com/apk/res/android"  
  4.   android:layout_width="fill_parent"  
  5.   android:layout_height="fill_parent">  
  6.   <Button    
  7.       android:id="@+id/loadMoreButton"    
  8.       android:layout_width="fill_parent"    
  9.       android:layout_height="wrap_content"  
  10.       android:text="查看更多..." />   
  11. </LinearLayout>  


商品类Goods.java的代码如下:

  1. package com.jemsn.util;  
  2.   
  3. public class Goods {  
  4.     private String Goods_ID;  
  5.     private String Gds_Code;  
  6.     private String Gds_Name;  
  7.     private String Gds_BarCode;  
  8.     private String Gds_Unit;  
  9.     private Double Gds_Ref;  
  10.     private String Gds_Price;  
  11.     private String Gds_PUnit;  
  12.     private String Gds_ClassID;  
  13.     private String Gds_GdsPY;  
  14.   
  15.     public String getGoods_ID() {  
  16.         return Goods_ID;  
  17.     }  
  18.   
  19.     public void setGoods_ID(String goods_ID) {  
  20.         Goods_ID = goods_ID;  
  21.     }  
  22.   
  23.     public String getGds_Code() {  
  24.         return Gds_Code;  
  25.     }  
  26.   
  27.     public void setGds_Code(String gds_Code) {  
  28.         Gds_Code = gds_Code;  
  29.     }  
  30.   
  31.     public String getGds_Name() {  
  32.         return Gds_Name;  
  33.     }  
  34.   
  35.     public void setGds_Name(String gds_Name) {  
  36.         Gds_Name = gds_Name;  
  37.     }  
  38.   
  39.     public String getGds_BarCode() {  
  40.         return Gds_BarCode;  
  41.     }  
  42.   
  43.     public void setGds_BarCode(String gds_BarCode) {  
  44.         Gds_BarCode = gds_BarCode;  
  45.     }  
  46.   
  47.     public String getGds_Unit() {  
  48.         return Gds_Unit;  
  49.     }  
  50.   
  51.     public void setGds_Unit(String gds_Unit) {  
  52.         Gds_Unit = gds_Unit;  
  53.     }  
  54.   
  55.     public Double getGds_Ref() {  
  56.         return Gds_Ref;  
  57.     }  
  58.   
  59.     public void setGds_Ref(Double gds_Ref) {  
  60.         Gds_Ref = gds_Ref;  
  61.     }  
  62.   
  63.     public String getGds_Price() {  
  64.         return Gds_Price;  
  65.     }  
  66.   
  67.     public void setGds_Price(String gds_Price) {  
  68.         Gds_Price = gds_Price;  
  69.     }  
  70.   
  71.     public String getGds_PUnit() {  
  72.         return Gds_PUnit;  
  73.     }  
  74.   
  75.     public void setGds_PUnit(String gds_PUnit) {  
  76.         Gds_PUnit = gds_PUnit;  
  77.     }  
  78.   
  79.     public String getGds_ClassID() {  
  80.         return Gds_ClassID;  
  81.     }  
  82.   
  83.     public void setGds_ClassID(String gds_ClassID) {  
  84.         Gds_ClassID = gds_ClassID;  
  85.     }  
  86.   
  87.     public String getGds_GdsPY() {  
  88.         return Gds_GdsPY;  
  89.     }  
  90.   
  91.     public void setGds_GdsPY(String gds_GdsPY) {  
  92.         Gds_GdsPY = gds_GdsPY;  
  93.     }  
  94.   
  95. }  


程序的主界面AndroidQueryActivity代码如下:

  1. package com.jemsn.android;  
  2.   
  3.   
  4. import java.util.ArrayList;  
  5. import java.util.List;  
  6.   
  7. import org.json.JSONArray;  
  8.   
  9. import com.jemsn.util.DialogUtil;  
  10. import com.jemsn.util.Goods;  
  11. import com.jemsn.util.WebServiceUtil;  
  12.   
  13. import android.app.Activity;  
  14. import android.os.Bundle;  
  15. import android.os.Handler;  
  16. import android.util.Log;  
  17. import android.view.View;  
  18. import android.view.ViewGroup;  
  19. import android.widget.AbsListView;  
  20. import android.widget.AbsListView.OnScrollListener;  
  21. import android.widget.BaseAdapter;  
  22. import android.widget.Button;  
  23. import android.widget.EditText;  
  24. import android.widget.ListView;  
  25. import android.widget.TextView;  
  26. import android.widget.Toast;  
  27.   
  28. public class AndroidQueryActivity extends Activity implements OnScrollListener {  
  29.   
  30.     private ListView list_View;  
  31.     private int visibleLastIndex = 0// 最后的可视项索引  
  32.     private int visibleItemCount; // 当前窗口可见项总数  
  33.     private int datasize = 100// 模拟数据集的条数  
  34.     private PaginationAdapter adapter;  
  35.     private View loadMoreView;  
  36.     private Button loadMoreButton,Search_Btn;  
  37.     private Handler handler = new Handler();  
  38.     private EditText SearcText;  
  39.   
  40.     /** Called when the activity is first created. */  
  41.     @Override  
  42.     public void onCreate(Bundle savedInstanceState) {  
  43.         super.onCreate(savedInstanceState);  
  44.         setContentView(R.layout.view_item);  
  45.         SearcText = (EditText) findViewById(R.id.text_SeGds);  
  46.         Search_Btn=(Button)findViewById(R.id.BtnGoods_Search);  
  47.         loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);  
  48.         loadMoreButton = (Button) loadMoreView  
  49.                 .findViewById(R.id.loadMoreButton);  
  50.         loadMoreButton.setOnClickListener(new View.OnClickListener() {  
  51.             @Override  
  52.             public void onClick(View v) {  
  53.                 loadMoreButton.setText("正在加载中..."); // 设置按钮文字  
  54.                 handler.postDelayed(new Runnable() {  
  55.   
  56.                     @Override  
  57.                     public void run() {  
  58.                         loadMoreData();  
  59.                         adapter.notifyDataSetChanged();  
  60.                         Toast.makeText(AndroidQueryActivity.this"数据加载完!", Toast.LENGTH_LONG).show();  
  61.                         loadMoreButton.setText("查看更多..."); // 恢复按钮文字  
  62.                     }  
  63.                 }, 2000);  
  64.   
  65.             }  
  66.         });  
  67.         Search_Btn.setOnClickListener(new View.OnClickListener() {  
  68.               
  69.             @Override  
  70.             public void onClick(View v) {  
  71.                 initializeAdapter();  
  72.                 list_View.setAdapter(adapter);  
  73.             }  
  74.         });  
  75.           
  76.         list_View = (ListView)findViewById(R.id.GoodsList);  
  77.           
  78.         list_View.addFooterView(loadMoreView); // 设置列表底部视图  
  79.         Log.e("WangYOUHellow","Goods");  
  80.         initializeAdapter();  
  81.           
  82.         list_View.setAdapter(adapter);  
  83.         Log.e("WangYOUHellow","Hellow");  
  84.         list_View.setOnScrollListener(this);  
  85.     }  
  86.   
  87.     @Override  
  88.     public void onScrollStateChanged(AbsListView view, int scrollState) {  
  89.         int itemsLastIndex = adapter.getCount() - 1// 数据集最后一项的索引  
  90.         int lastIndex = itemsLastIndex + 1;  
  91.         if (scrollState == OnScrollListener.SCROLL_STATE_IDLE  
  92.                 && visibleLastIndex == lastIndex) {  
  93.             // 如果是自动加载,可以在这里放置异步加载数据的代码  
  94.         }  
  95.     }  
  96.   
  97.     @Override  
  98.     public void onScroll(AbsListView view, int firstVisibleItem,  
  99.             int visibleItemCount, int totalItemCount) {  
  100.         this.visibleItemCount = visibleItemCount;  
  101.         visibleLastIndex = firstVisibleItem + visibleItemCount - 1;  
  102.   
  103.         // 如果所有的记录选项等于数据集的条数,则移除列表底部视图  
  104.         if (totalItemCount == datasize + 1) {  
  105.             //list_View.removeFooterView(loadMoreView);  
  106.             Toast.makeText(this"数据全部加载完!", Toast.LENGTH_LONG).show();  
  107.         }  
  108.     }  
  109.   
  110.     /** 
  111.      * 初始化ListView的适配器 
  112.      */  
  113.     private void initializeAdapter() {  
  114.         Log.e("WangYOUHellow","Bad");  
  115.         try {  
  116.             String Parame = SearcText.getText().toString();  
  117.             String ReturnStr = WebServiceUtil  
  118.                     .getCityListByProvince(Parame, "1");  
  119.             //DialogUtil.showDialog(this,ReturnStr, false);  
  120.             JSONArray jsonArray;  
  121.             // DialogUtil.showDialog(this, ReturnStr, false);  
  122.             if (ReturnStr.contentEquals("anyType{}")) {  
  123.                 jsonArray = new JSONArray();  
  124.             } else {  
  125.                 jsonArray = new JSONArray(ReturnStr);  
  126.             }  
  127.             // 向指定URL发送请求,并把服务器响应转换成JSONArray对象  
  128.   
  129.             // 将JSONArray包装成Adapter  
  130.             List<Goods> GoodsArry = new ArrayList<Goods>();  
  131.   
  132.             for (int i = 0; i<jsonArray.length(); i++) {  
  133.                 Goods items = new Goods();  
  134.                 items.setGds_Name(jsonArray.optJSONObject(i).getString(  
  135.                         "GdsName"));  
  136.                 items.setGds_Code(jsonArray.optJSONObject(i).getString(  
  137.                         "GdsCode"));  
  138.                 GoodsArry.add(items);  
  139.             }  
  140.             adapter = new PaginationAdapter(GoodsArry);  
  141.         } catch (Exception e) {  
  142.             //DialogUtil.showDialog(this, e.getMessage(), false);  
  143.             e.printStackTrace();  
  144.         }  
  145.   
  146.     }  
  147.   
  148.     /** 
  149.      * 加载更多数据 
  150.      */  
  151.     private void loadMoreData() {  
  152.         int count = adapter.getCount();  
  153.   
  154.         try {  
  155.   
  156.             String Parame = SearcText.getText().toString();  
  157.             String pageSize = String.valueOf(count / 10 + 1);  
  158.             String ReturnStr = WebServiceUtil.getCityListByProvince(Parame,  
  159.                     pageSize);  
  160.             JSONArray jsonArray;  
  161.             // DialogUtil.showDialog(this, ReturnStr, false);  
  162.             if (ReturnStr.contentEquals("anyType{}")) {  
  163.                 jsonArray = new JSONArray();  
  164.             } else {  
  165.                 jsonArray = new JSONArray(ReturnStr);  
  166.             }  
  167.             // 向指定URL发送请求,并把服务器响应转换成JSONArray对象  
  168.   
  169.             // 将JSONArray包装成Adapter  
  170.             for (int i = 1; jsonArray.length() <= 10; i++) {  
  171.                 Goods items = new Goods();  
  172.                 items.setGds_Name(jsonArray.optJSONObject(i).getString(  
  173.                         "GdsName"));  
  174.                 items.setGds_Code(jsonArray.optJSONObject(i).getString(  
  175.                         "GdsCode"));  
  176.                 adapter.addNewsItem(items);  
  177.             }  
  178.         } catch (Exception e) {  
  179.             //DialogUtil.showDialog(this, e.getMessage(), false);  
  180.             e.printStackTrace();  
  181.         }  
  182.   
  183.     }  
  184.   
  185.     class PaginationAdapter extends BaseAdapter {  
  186.   
  187.         List<Goods> newsItems;  
  188.   
  189.         public PaginationAdapter(List<Goods> newsitems) {  
  190.             this.newsItems = newsitems;  
  191.         }  
  192.   
  193.         @Override  
  194.         public int getCount() {  
  195.             return newsItems.size();  
  196.         }  
  197.   
  198.         @Override  
  199.         public Object getItem(int position) {  
  200.             return newsItems.get(position);  
  201.         }  
  202.   
  203.         @Override  
  204.         public long getItemId(int position) {  
  205.             return position;  
  206.         }  
  207.   
  208.         @Override  
  209.         public View getView(int position, View view, ViewGroup parent) {  
  210.             if (view == null) {  
  211.                 view = getLayoutInflater().inflate(R.layout.list_item, null);  
  212.             }  
  213.   
  214.             // 商品名称  
  215.             TextView tvGdsName = (TextView) view.findViewById(R.id.Gds_Name);  
  216.             tvGdsName.setText(newsItems.get(position).getGds_Name());  
  217.             // 商品编码  
  218.             TextView tvGdsCode = (TextView) view.findViewById(R.id.Gds_Code);  
  219.             tvGdsCode.setText(newsItems.get(position).getGds_Code());  
  220.   
  221.             return view;  
  222.         }  
  223.   
  224.         /** 
  225.          * 添加数据列表项 
  226.          *  
  227.          * @param newsitem 
  228.          */  
  229.         public void addNewsItem(Goods newsitem) {  
  230.             newsItems.add(newsitem);  
  231.         }  
  232.   
  233.     }  
  234.   
  235. }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值