list_item.xml代码如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="horizontal">
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical">
- <TextView
- android:id="@+id/Gds_Name"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:textSize="20px"
- />
- <TextView
- android:id="@+id/Gds_Code"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"/>
- </LinearLayout>
- </LinearLayout>
显示加载更多的布局代码loadmore.xml如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button
- android:id="@+id/loadMoreButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="查看更多..." />
- </LinearLayout>
商品类Goods.java的代码如下:
- package com.jemsn.util;
- public class Goods {
- private String Goods_ID;
- private String Gds_Code;
- private String Gds_Name;
- private String Gds_BarCode;
- private String Gds_Unit;
- private Double Gds_Ref;
- private String Gds_Price;
- private String Gds_PUnit;
- private String Gds_ClassID;
- private String Gds_GdsPY;
- public String getGoods_ID() {
- return Goods_ID;
- }
- public void setGoods_ID(String goods_ID) {
- Goods_ID = goods_ID;
- }
- public String getGds_Code() {
- return Gds_Code;
- }
- public void setGds_Code(String gds_Code) {
- Gds_Code = gds_Code;
- }
- public String getGds_Name() {
- return Gds_Name;
- }
- public void setGds_Name(String gds_Name) {
- Gds_Name = gds_Name;
- }
- public String getGds_BarCode() {
- return Gds_BarCode;
- }
- public void setGds_BarCode(String gds_BarCode) {
- Gds_BarCode = gds_BarCode;
- }
- public String getGds_Unit() {
- return Gds_Unit;
- }
- public void setGds_Unit(String gds_Unit) {
- Gds_Unit = gds_Unit;
- }
- public Double getGds_Ref() {
- return Gds_Ref;
- }
- public void setGds_Ref(Double gds_Ref) {
- Gds_Ref = gds_Ref;
- }
- public String getGds_Price() {
- return Gds_Price;
- }
- public void setGds_Price(String gds_Price) {
- Gds_Price = gds_Price;
- }
- public String getGds_PUnit() {
- return Gds_PUnit;
- }
- public void setGds_PUnit(String gds_PUnit) {
- Gds_PUnit = gds_PUnit;
- }
- public String getGds_ClassID() {
- return Gds_ClassID;
- }
- public void setGds_ClassID(String gds_ClassID) {
- Gds_ClassID = gds_ClassID;
- }
- public String getGds_GdsPY() {
- return Gds_GdsPY;
- }
- public void setGds_GdsPY(String gds_GdsPY) {
- Gds_GdsPY = gds_GdsPY;
- }
- }
程序的主界面AndroidQueryActivity代码如下:
- package com.jemsn.android;
- import java.util.ArrayList;
- import java.util.List;
- import org.json.JSONArray;
- import com.jemsn.util.DialogUtil;
- import com.jemsn.util.Goods;
- import com.jemsn.util.WebServiceUtil;
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AbsListView.OnScrollListener;
- import android.widget.BaseAdapter;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- public class AndroidQueryActivity extends Activity implements OnScrollListener {
- private ListView list_View;
- private int visibleLastIndex = 0; // 最后的可视项索引
- private int visibleItemCount; // 当前窗口可见项总数
- private int datasize = 100; // 模拟数据集的条数
- private PaginationAdapter adapter;
- private View loadMoreView;
- private Button loadMoreButton,Search_Btn;
- private Handler handler = new Handler();
- private EditText SearcText;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.view_item);
- SearcText = (EditText) findViewById(R.id.text_SeGds);
- Search_Btn=(Button)findViewById(R.id.BtnGoods_Search);
- loadMoreView = getLayoutInflater().inflate(R.layout.loadmore, null);
- loadMoreButton = (Button) loadMoreView
- .findViewById(R.id.loadMoreButton);
- loadMoreButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- loadMoreButton.setText("正在加载中..."); // 设置按钮文字
- handler.postDelayed(new Runnable() {
- @Override
- public void run() {
- loadMoreData();
- adapter.notifyDataSetChanged();
- Toast.makeText(AndroidQueryActivity.this, "数据加载完!", Toast.LENGTH_LONG).show();
- loadMoreButton.setText("查看更多..."); // 恢复按钮文字
- }
- }, 2000);
- }
- });
- Search_Btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- initializeAdapter();
- list_View.setAdapter(adapter);
- }
- });
- list_View = (ListView)findViewById(R.id.GoodsList);
- list_View.addFooterView(loadMoreView); // 设置列表底部视图
- Log.e("WangYOUHellow","Goods");
- initializeAdapter();
- list_View.setAdapter(adapter);
- Log.e("WangYOUHellow","Hellow");
- list_View.setOnScrollListener(this);
- }
- @Override
- public void onScrollStateChanged(AbsListView view, int scrollState) {
- int itemsLastIndex = adapter.getCount() - 1; // 数据集最后一项的索引
- int lastIndex = itemsLastIndex + 1;
- if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
- && visibleLastIndex == lastIndex) {
- // 如果是自动加载,可以在这里放置异步加载数据的代码
- }
- }
- @Override
- public void onScroll(AbsListView view, int firstVisibleItem,
- int visibleItemCount, int totalItemCount) {
- this.visibleItemCount = visibleItemCount;
- visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
- // 如果所有的记录选项等于数据集的条数,则移除列表底部视图
- if (totalItemCount == datasize + 1) {
- //list_View.removeFooterView(loadMoreView);
- Toast.makeText(this, "数据全部加载完!", Toast.LENGTH_LONG).show();
- }
- }
- /**
- * 初始化ListView的适配器
- */
- private void initializeAdapter() {
- Log.e("WangYOUHellow","Bad");
- try {
- String Parame = SearcText.getText().toString();
- String ReturnStr = WebServiceUtil
- .getCityListByProvince(Parame, "1");
- //DialogUtil.showDialog(this,ReturnStr, false);
- JSONArray jsonArray;
- // DialogUtil.showDialog(this, ReturnStr, false);
- if (ReturnStr.contentEquals("anyType{}")) {
- jsonArray = new JSONArray();
- } else {
- jsonArray = new JSONArray(ReturnStr);
- }
- // 向指定URL发送请求,并把服务器响应转换成JSONArray对象
- // 将JSONArray包装成Adapter
- List<Goods> GoodsArry = new ArrayList<Goods>();
- for (int i = 0; i<jsonArray.length(); i++) {
- Goods items = new Goods();
- items.setGds_Name(jsonArray.optJSONObject(i).getString(
- "GdsName"));
- items.setGds_Code(jsonArray.optJSONObject(i).getString(
- "GdsCode"));
- GoodsArry.add(items);
- }
- adapter = new PaginationAdapter(GoodsArry);
- } catch (Exception e) {
- //DialogUtil.showDialog(this, e.getMessage(), false);
- e.printStackTrace();
- }
- }
- /**
- * 加载更多数据
- */
- private void loadMoreData() {
- int count = adapter.getCount();
- try {
- String Parame = SearcText.getText().toString();
- String pageSize = String.valueOf(count / 10 + 1);
- String ReturnStr = WebServiceUtil.getCityListByProvince(Parame,
- pageSize);
- JSONArray jsonArray;
- // DialogUtil.showDialog(this, ReturnStr, false);
- if (ReturnStr.contentEquals("anyType{}")) {
- jsonArray = new JSONArray();
- } else {
- jsonArray = new JSONArray(ReturnStr);
- }
- // 向指定URL发送请求,并把服务器响应转换成JSONArray对象
- // 将JSONArray包装成Adapter
- for (int i = 1; jsonArray.length() <= 10; i++) {
- Goods items = new Goods();
- items.setGds_Name(jsonArray.optJSONObject(i).getString(
- "GdsName"));
- items.setGds_Code(jsonArray.optJSONObject(i).getString(
- "GdsCode"));
- adapter.addNewsItem(items);
- }
- } catch (Exception e) {
- //DialogUtil.showDialog(this, e.getMessage(), false);
- e.printStackTrace();
- }
- }
- class PaginationAdapter extends BaseAdapter {
- List<Goods> newsItems;
- public PaginationAdapter(List<Goods> newsitems) {
- this.newsItems = newsitems;
- }
- @Override
- public int getCount() {
- return newsItems.size();
- }
- @Override
- public Object getItem(int position) {
- return newsItems.get(position);
- }
- @Override
- public long getItemId(int position) {
- return position;
- }
- @Override
- public View getView(int position, View view, ViewGroup parent) {
- if (view == null) {
- view = getLayoutInflater().inflate(R.layout.list_item, null);
- }
- // 商品名称
- TextView tvGdsName = (TextView) view.findViewById(R.id.Gds_Name);
- tvGdsName.setText(newsItems.get(position).getGds_Name());
- // 商品编码
- TextView tvGdsCode = (TextView) view.findViewById(R.id.Gds_Code);
- tvGdsCode.setText(newsItems.get(position).getGds_Code());
- return view;
- }
- /**
- * 添加数据列表项
- *
- * @param newsitem
- */
- public void addNewsItem(Goods newsitem) {
- newsItems.add(newsitem);
- }
- }
- }