android分页查询功能工具类的实现

http://blog.csdn.net/jindegegesun/article/details/7287495

在一个项目中,常常需要频繁的分页,所以有必要实现一个封装的类来统一管理实现。

首先看下效果图:

   从上图我们可以看出,这里是将listview中的数据通过底下的分页工具实现分页显示。那我们接下来先贴出代码:

[html]  view plain copy print ?
  1. 1、实现布局,这里有两个布局文件,第一个主布局文件main.xml  
  2.   
  3. <RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"  
  4.                 android:id="@+id/whole_region"  
  5.                 android:layout_width="fill_parent"  
  6.                 android:layout_height="fill_parent"  
  7.                 android:orientation="vertical" >  
  8.   
  9.                 <HorizontalScrollView  
  10.                     android:id="@+id/HorizontalScrollView01"  
  11.                     android:layout_width="fill_parent"  
  12.                     android:layout_height="wrap_content"  
  13.                     android:layout_above="@+id/pagebtn"  
  14.                     android:layout_alignParentTop="true" >  
  15.   
  16.                     <LinearLayout  
  17.                         android:layout_width="wrap_content"  
  18.                         android:layout_height="fill_parent"  
  19.                         android:orientation="vertical" >  
  20.   
  21.                         <LinearLayout  
  22.                             android:id="@+id/list_view_title"  
  23.                             android:layout_width="wrap_content"  
  24.                             android:layout_height="wrap_content"  
  25.                             android:orientation="horizontal">                          
  26.                         </LinearLayout>  
  27.   
  28.                         <ListView  
  29.                             android:id="@+id/list"  
  30.                             android:layout_width="wrap_content"  
  31.                             android:layout_height="wrap_content"  
  32.                             android:fadeScrollbars="false" >  
  33.                         </ListView>  
  34.                     </LinearLayout>  
  35.                 </HorizontalScrollView>  
  36.   
  37.                 <LinearLayout  
  38.                     android:id="@id/pagebtn"  
  39.                     android:layout_width="fill_parent"  
  40.                     android:layout_height="wrap_content"  
  41.                     android:layout_alignParentBottom="true"  
  42.                     android:orientation="vertical" >  
  43.   
  44.                     <include layout="@layout/pagebutton" />  
  45.                 </LinearLayout>  
  46.             </RelativeLayout>  
  47.   
  48. 第二个布局文件pagebutton.xml.这个文件嵌套在第一个主文件中。  
[html]  view plain copy print ?
  1. <pre class="html" name="code"><?xml version="1.0" encoding="utf-8"?>  
  2. <merge xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  4.     <TableLayout android:id="@+id/TableLayout1"  
  5.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  6.         android:stretchColumns="0,1,2,3,4,5,6,7" android:shrinkColumns="0,1,2,3,4,5,6,7"   
  7.         android:paddingTop="5dip" android:paddingLeft="5dip" android:paddingRight="5dip">  
  8.         <TableRow>  
  9.             <TextView android:text="共" android:layout_width="wrap_content"  
  10.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  11.             <TextView android:id="@+id/total_page_num" android:minWidth="30dp"  
  12.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  13.                 android:textColor="#C71585"  
  14.                 android:textSize="18sp" />  
  15.             <TextView android:text="页" android:layout_width="wrap_content"  
  16.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  17.             <TextView android:id="@+id/total_record_num"  
  18.                 android:minWidth="60dp" android:layout_width="wrap_content"  
  19.                 android:layout_height="wrap_content" android:textColor="#C71585" android:textSize="18sp" />  
  20.             <TextView android:text="条," android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  22.             <TextView android:text="每页" android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  24.             <TextView android:id="@+id/record_num_each_page"  
  25.                 android:layout_width="wrap_content"   
[html]  view plain copy print ?
  1. android:layout_height="wrap_content"                        
[html]  view plain copy print ?
  1.                                     android:textColor="#C71585"  
  2.                 android:textSize="18sp" />  
  3.             <TextView android:text="条" android:layout_width="wrap_content"  
  4.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  5.         </TableRow>  
  6.     </TableLayout>  
  7.     <TableLayout android:id="@+id/TableLayout1"  
  8.         android:layout_width="fill_parent" android:layout_height="wrap_content"  
  9.         android:stretchColumns="0,1,2,3,4,5,6,7" android:shrinkColumns="0,1,2,3,4,5,6,7,8" android:paddingTop="5dip">  
  10.         <TableRow>  
  11.             <Button android:id="@+id/first_page" android:text="|<"  
  12.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  13.                 android:textSize="18sp" />  
  14.             <Button android:id="@+id/page_up" android:text=" < "  
  15.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  16.                 android:textSize="18sp" />  
  17.             <Button android:id="@+id/page_down" android:text=" > "  
  18.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  19.                 android:textSize="18sp" />  
  20.             <Button android:id="@+id/last_page" android:text=">|"  
  21.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  22.                 android:textSize="18sp" />  
  23.             <TextView android:text="第" android:layout_width="wrap_content"  
  24.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  25.             <EditText android:id="@+id/current_page" android:maxWidth="40dip"  
  26.                 android:singleLine="true" android:layout_width="wrap_content"  
  27.                 android:layout_height="wrap_content"  android:textColor="#C71585" android:textSize="18sp" />  
  28.             <TextView android:text="页" android:layout_width="wrap_content"  
  29.                 android:layout_height="wrap_content" android:textSize="18sp" />  
  30.             <Button android:id="@+id/to_page" android:text="转到"  
  31.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  32.                 android:textSize="18sp" />  
  33.         </TableRow>  
  34.     </TableLayout>  
  35. </merge>  

 

第二步:新建一个activity,用来显示界面

[java]  view plain copy print ?
  1. public class BuilderList1 extends QueryContextActivity {  
  2.     private PagedQueryResultHelper pagedQueryResultHelper;  
  3.     private Map<String, Object> params;  
  4.     private ListView list;  
  5.     private List<Integer> columnWidths;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         // TODO Auto-generated method stub  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.mynotifylist);  
  12.         setOnClickListener();  
  13.           
  14.   
  15.         columnWidths = new ArrayList<Integer>();  
  16.         int weight = this.getWindowManager().getDefaultDisplay().getWidth();  
  17.         columnWidths.add(weight);  
  18.   
  19.         pagedQueryResultHelper = new PagedQueryResultHelper(this);  
  20.         pagedQueryResultHelper.setTitleCellWidth(columnWidths);  
[java]  view plain copy print ?
  1. //这个方法是用来查询数据并显示在LISTVIEW中的,我们这次的重点不在这里,我只列出不详讲,有兴趣的朋友可以自己研究下listview               query();  
[java]  view plain copy print ?
  1.     }  
  2.   
  3.     <p>        protected void query() {  
  4.                 params = new HashMap<String, Object>();  
  5.                 params.put("userName", UserInfo.getUserName());</p><p>                pagedQueryResultHelper.setQueryParams("getBuilderList", params,  
  6.                "startNum""endNum""MyNotifyList", R.array.build_list_name, R.array.build_list);  
  7.              if (pagedQueryResultHelper.initialQuery() == null) {  
  8.                              new AlertDialog.Builder(this)  
  9.                             .setTitle(R.string.message_title)  
  10.                             .setMessage("没有查询到相关信息,请返回!")  
  11.                             .setPositiveButton(R.string.OK_text, null)  
  12.                             .setPositiveButton(R.string.OK_text,  
  13.                            new DialogInterface.OnClickListener() {  
  14.                        @Override  
  15.                     public void onClick(DialogInterface dialog, int which) {</p><p>                            BuilderList1.this.finish();</p><p>                     } }).show();  
  16.   };</p><p>             list = (ListView) findViewById(R.id.list);</p><p>             list.setOnItemClickListener(new OnItemClickListener() {  
  17.              @Override  
  18.             public void onItemClick(AdapterView<?> adapterView, View view, int index, long arg3) {</p><p>                  List<Map<String, Object>> data = pagedQueryResultHelper .getWholeResult();  
  19.                   Intent intent = new Intent(BuilderList1.this,FloorInfo.class);  
  20.                   intent.putExtra("value", (Serializable) data.get(index));  
  21.                   intent.putExtra("buildid", data.get(index).get("BUILDID").toString());  
  22.                   startActivity(intent); }});  
  23.  }</p><p>             @Override  
  24.             public void onClick(View arg0) {  
  25.               switch (arg0.getId()) {  
  26.               case R.id.first_page:  
  27.                pagedQueryResultHelper.firstPage();  
  28.                   break;  
  29.               case R.id.page_down:  
  30.                pagedQueryResultHelper.pageDown();  
  31.                   break;  
  32.               case R.id.page_up:  
  33.                pagedQueryResultHelper.pageUp();  
  34.                 break;  
  35.               case R.id.last_page:  
  36.                pagedQueryResultHelper.lastPage();  
  37.                  break;  
  38.               case R.id.to_page:  
  39.                pagedQueryResultHelper.toPage();  
  40.                 break;  
  41.         }  
  42.     }</p><p>        @Override  
  43.               protected void clear() {  
  44.    }</p><p>       @Override  
  45.     protected void setOnClickListener() {  
  46.    }</p><p>}</p><p> </p>第三步:实现这个工具类,这个类代码有些多  
[java]  view plain copy print ?
  1. <pre class="java" name="code">package org.Base.Utils;  
  2.   
  3. import java.util.Arrays;  
  4. import java.util.List;  
  5. import java.util.Map;  
  6.   
  7. import org.Base.Activities.QueryContextActivity;  
  8. import org.Base.Container.DataTableAdapter;  
  9. import org.Base.Container.myListViewTitle;  
  10. import org.Base.Webservice.WSObjectListUtil;  
  11. import org.Base.Webservice.WSObjectMapUtil;  
  12. import org.Base.Webservice.WSObjectUtil;  
  13. import org.Base.Webservice.WSUtil;  
  14. import org.Base.Webservice.WebServiceConfig;  
  15. import org.DigitalCM.R;  
  16. import org.ksoap2.serialization.SoapObject;  
  17.   
  18. import android.app.AlertDialog;  
  19. import android.util.Log;  
  20. import android.widget.Button;  
  21. import android.widget.EditText;  
  22. import android.widget.ListView;  
  23. import android.widget.TextView;  
  24.   
  25. public class PagedQueryResultHelper {  
  26.     private final static int initialFromRecordNum = 1;//每页从第1条开始  
  27.     private final static int initialToRecordNum = 20;//每页可以显示20条  
  28.   
  29.     public PagedQueryResultHelper(QueryContextActivity contextActivity) {//构造函数用来初始化,主要是用来接收传过来的activity做为上下文对象  
  30.         this.contextActivity = contextActivity;  
  31.   
  32.         findViews();//调用此方法找到传过来的activity的控件对象  
  33.         setOnClickListener();//给控件设置监听  
  34.         if (currentPageText != null) {//如果所有页面只有一页,那就只显示一页  
  35.             setCurrentPage(1);  
  36.         }  
  37.     }  
  38.   
  39.     public List<List<String>> initialQuery() {  
  40.         setRecordNumEachPage(initialToRecordNum - initialFromRecordNum + 1);//设置每页的记录条数  
  41.         return query(initialFromRecordNum, initialToRecordNum);  
  42.   
  43.     }  
  44.   
  45.     public List<List<String>> initialQuery01() {  
  46.         return query(methodName, params);  
  47.     }  
  48.   
  49.     private void setCurrentPage(int currentPage) {//设置当前页  
  50.         this.currentPage = currentPage;  
  51.         currentPageText.setText(String.valueOf(currentPage));  
  52.     }  
  53.   
  54.     public void setMarkable(boolean bMarkable) {//这个是用来标记记录的  
  55.         this.bMarkable = bMarkable;  
  56.     }  
  57.   
  58.     public void setRecordNumEachPage(int recordNumEachPage) {);//设置每页的记录条数  
  59.         this.recordNumEachPage = recordNumEachPage;  
  60.         recordNumEachPageText.setText(String.valueOf(recordNumEachPage));  
  61.     }  
  62.   
  63.     public void setTotalRecord(int totalRecord) {//设置总记录数  
  64.         this.totalRecord = totalRecord;  
  65.         totalRecordNumText.setText(String.valueOf(totalRecord));  
  66.         getTotalPageNumText();  
  67.     }  
  68.   
  69.     private QueryContextActivity contextActivity;  
  70.     private TextView totalPageNumText;  
  71.     private TextView totalRecordNumText;  
  72.     private TextView recordNumEachPageText;  
  73.     private Button firstPage;  
  74.     private Button pageDown;  
  75.     private Button pageUp;  
  76.     private Button lastPage;  
  77.     private EditText currentPageText;  
  78.     private Button toPage;  
  79.   
  80.     private int totalPage;  
  81.     private int totalRecord;  
  82.     private int recordNumEachPage;  
  83.     private int currentPage;  
  84.     private String fromRecordNumField;  
  85.     private String toRecordNumField;  
  86.     private String methodName;  
  87.     private Map<String, Object> params;  
  88.     private String contentTableName;  
  89.     // private String countTableName;  
  90.     private int headerNameId;  
  91.     private int headerId;  
  92.     private List<Integer> columnWidths = null;  
  93.     private boolean bSetTitleValue = false;  
  94.     private List<Map<String, Object>> wholeresult = null;  
  95.     private List<List<String>> rows = null;  
  96.     private boolean bMarkable = false;  
  97.     private DataTableAdapter dataTableAdapter;  
  98.   
  99.     public void setQueryParams(String methodName, Map<String, Object> params,  
  100.             String fromRecordNumField, String toRecordNumField,  
  101.             String contentTableName, int headerNameId, int headerId) {  
  102.         this.methodName = methodName;  
  103.         this.params = params;  
  104.         this.fromRecordNumField = fromRecordNumField;  
  105.         this.toRecordNumField = toRecordNumField;  
  106.         this.contentTableName = contentTableName;  
  107.         // this.countTableName = countTableName;  
  108.         this.headerNameId = headerNameId;  
  109.         this.headerId = headerId;  
  110.     }  
  111.   
  112.     public void setQueryParams(String methodName, Map<String, Object> params,  
  113.             String fromRecordNumField, String toRecordNumField,  
  114.             String contentTableName) {  
  115.         this.methodName = methodName;  
  116.         this.params = params;  
  117.         this.fromRecordNumField = fromRecordNumField;  
  118.         this.toRecordNumField = toRecordNumField;  
  119.         this.contentTableName = contentTableName;  
  120.         // this.countTableName = countTableName;  
  121.   
  122.     }  
  123.   
  124.     public void setQueryParams(String methodName, Map<String, Object> params,  
  125.             String contentTableName, int headerNameId, int headerId) {  
  126.         this.methodName = methodName;  
  127.         this.params = params;  
  128.   
  129.         this.contentTableName = contentTableName;  
  130.   
  131.         this.headerNameId = headerNameId;  
  132.         this.headerId = headerId;  
  133.   
  134.     }  
  135.   
  136.     public void setTitleCellWidth(List<Integer> columnWidths) {  
  137.         this.columnWidths = columnWidths;  
  138.     }  
  139.   
  140.     /* 
  141.      * public int getCurrentPage(){ return currentPage; } 
  142.      */  
  143.   
  144.     private void findViews() {  
  145.         totalPageNumText = (TextView) contextActivity  
  146.                 .findViewById(R.id.total_page_num);  
  147.         totalRecordNumText = (TextView) contextActivity  
  148.                 .findViewById(R.id.total_record_num);  
  149.         recordNumEachPageText = (TextView) contextActivity  
  150.                 .findViewById(R.id.record_num_each_page);  
  151.         firstPage = (Button) contextActivity.findViewById(R.id.first_page);  
  152.         pageDown = (Button) contextActivity.findViewById(R.id.page_down);  
  153.         pageUp = (Button) contextActivity.findViewById(R.id.page_up);  
  154.         lastPage = (Button) contextActivity.findViewById(R.id.last_page);  
  155.         currentPageText = (EditText) contextActivity  
  156.                 .findViewById(R.id.current_page);  
  157.         toPage = (Button) contextActivity.findViewById(R.id.to_page);  
  158.     }  
  159.   
  160.     private void setOnClickListener() {  
  161.         if (firstPage != null) {  
  162.             firstPage.setOnClickListener(contextActivity);  
  163.         }  
  164.         if (pageDown != null) {  
  165.             pageDown.setOnClickListener(contextActivity);  
  166.         }  
  167.         if (pageUp != null) {  
  168.             pageUp.setOnClickListener(contextActivity);  
  169.         }  
  170.         if (lastPage != null) {  
  171.             lastPage.setOnClickListener(contextActivity);  
  172.         }  
  173.         if (toPage != null) {  
  174.             toPage.setOnClickListener(contextActivity);  
  175.         }  
  176.     }  
  177.   
  178.     private void getRecordNumEachPageText() {  
  179.         try {  
  180.             recordNumEachPage = Integer.parseInt(recordNumEachPageText  
  181.                     .getText().toString());  
  182.         } catch (NumberFormatException e) {  
  183.             Log.e("NumberFormatException", e.getMessage());  
  184.         }  
  185.     }  
  186.   
  187.     private boolean outOfEachPageRange() {  
  188.         recordNumEachPage = Integer.parseInt(recordNumEachPageText.getText()  
  189.                 .toString());  
  190.         if (recordNumEachPage > 20 || recordNumEachPage < 1) {  
  191.             return true;  
  192.         }  
  193.         return false;  
  194.     }  
  195.   
  196.     private void getTotalPageNumText() {  
  197.         getRecordNumEachPageText();  
  198.         if (recordNumEachPage == 0)  
  199.             return;  
  200.         if (totalRecord == 0) {  
  201.             totalPage = 0;  
  202.             totalPageNumText.setText(String.valueOf(0));  
  203.             return;  
  204.         }  
  205.         if (totalRecord % recordNumEachPage == 0) {//java中分页常用到的计算公式  
  206.             totalPage = totalRecord / recordNumEachPage;  
  207.         } else {  
  208.             totalPage = totalRecord / recordNumEachPage + 1;  
  209.         }  
  210.         totalPageNumText.setText(String.valueOf(totalPage));  
  211.     }  
  212.   
  213.     public void pageDown() {  
  214.         if (outOfEachPageRange()) {  
  215.             AlertDialog.Builder dialog = new AlertDialog.Builder(  
  216.                     contextActivity);  
  217.             dialog.setTitle("提示:").setMessage("每页显示条数在1~20之间!")  
  218.                     .setPositiveButton("确认"null).show();  
  219.             return;  
  220.         }  
  221.         if (currentPage != totalPage) {  
  222.             setCurrentPage(++currentPage);  
  223.             getTotalPageNumText();  
  224.             query(recordNumEachPage * (currentPage - 1) + 1, recordNumEachPage  
  225.                     * currentPage);  
  226.         }  
  227.     }  
  228.   
  229.     public void pageUp() {  
  230.         if (outOfEachPageRange()) {  
  231.             AlertDialog.Builder dialog = new AlertDialog.Builder(  
  232.                     contextActivity);  
  233.             dialog.setTitle("提示:").setMessage("每页显示条数在1~20之间!")  
  234.                     .setPositiveButton("确认"null).show();  
  235.             return;  
  236.         }  
  237.         if (currentPage != 1 && currentPage != 0) {  
  238.             setCurrentPage(--currentPage);  
  239.             getTotalPageNumText();  
  240.             query(recordNumEachPage * (currentPage - 1) + 1, recordNumEachPage  
  241.                     * currentPage);  
  242.         }  
  243.     }  
  244.   
  245.     public void firstPage() {  
  246.         if (outOfEachPageRange()) {  
  247.             AlertDialog.Builder dialog = new AlertDialog.Builder(  
  248.                     contextActivity);  
  249.             dialog.setTitle("提示:").setMessage("每页显示条数在1~20之间!")  
  250.                     .setPositiveButton("确认"null).show();  
  251.             return;  
  252.         }  
  253.         getTotalPageNumText();  
  254.         if (totalPage > 1) {  
  255.             setCurrentPage(1);  
  256.             query(1, recordNumEachPage);  
  257.         }  
  258.     }  
  259.   
  260.     public void lastPage() {  
  261.         if (outOfEachPageRange()) {  
  262.             AlertDialog.Builder dialog = new AlertDialog.Builder(  
  263.                     contextActivity);  
  264.             dialog.setTitle("提示:").setMessage("每页显示条数在1~20之间!")  
  265.                     .setPositiveButton("确认"null).show();  
  266.             return;  
  267.         }  
  268.         getTotalPageNumText();  
  269.         if (totalPage != 1 && totalPage != 0) {  
  270.             setCurrentPage(totalPage);  
  271.             query(recordNumEachPage * (totalPage - 1) + 1, totalRecord);  
  272.         }  
  273.     }  
  274.   
  275.     public void toPage() {  
  276.         if (outOfEachPageRange()) {  
  277.             AlertDialog.Builder dialog = new AlertDialog.Builder(  
  278.                     contextActivity);  
  279.             dialog.setTitle("提示:").setMessage("每页显示条数在1~20之间!")  
  280.                     .setPositiveButton("确认"null).show();  
  281.             return;  
  282.         }  
  283.         try {  
  284.             currentPage = Integer  
  285.                     .parseInt(currentPageText.getText().toString());  
  286.         } catch (NumberFormatException e) {  
  287.             Log.e("NumberFormatException", e.getMessage());  
  288.             return;  
  289.         }  
  290.         getTotalPageNumText();  
  291.         query(recordNumEachPage * (currentPage - 1) + 1, recordNumEachPage  
  292.                 * currentPage);  
  293.         currentPageText.setText(String.valueOf(currentPage));  
  294.     }  
  295.   
  296.     public List<List<String>> query(int fromRecordNum, int toRecordNum) {  
  297.         return query(methodName, params, fromRecordNumField, toRecordNumField,  
  298.                 fromRecordNum, toRecordNum);  
  299.     }  
  300.   
  301.     private List<List<String>> query(String methodName,  
  302.             Map<String, Object> params, String fromRecordNumField,  
  303.             String toRecordNumField, int fromRecordNum, int toRecordNum) {  
  304.         params.put(fromRecordNumField, fromRecordNum);  
  305.         params.put(toRecordNumField, toRecordNum);  
  306.   
  307.         return query(methodName, params);  
  308.     }  
  309.   
  310.     public List<Map<String, Object>> getWholeResult() {  
  311.         return wholeresult;  
  312.     }  
  313.   
  314.     public List<Map<String, Object>> getResult() {  
  315.         return wholeresult;  
  316.     }  
  317.   
  318.     public List<Boolean> getCheckBoxState() {  
  319.         return dataTableAdapter.getCheckBoxState();  
  320.     }  
  321.   
  322.     public List<List<String>> query(String methodName,  
  323.             Map<String, Object> params) {//此方法要重点注意,因为这里是根据webservice查询到的数据,所以要传递来方法和参数  
  324.         SoapObject result = null;  
  325.   
  326.         if (methodName == null) {  
  327.             if (bSetTitleValue == false) {//下面是实现listview的标题显示  
  328.                 bSetTitleValue = true;  
  329.                 myListViewTitle listViewTitle = new myListViewTitle(  
  330.                         contextActivity);  
  331.                 if (this.columnWidths != null) {  
  332.                     listViewTitle.setTitleCellWidth(this.columnWidths);  
  333.                 }  
  334.                 List<String> headerList = Arrays.asList(contextActivity  
  335.                         .getResources().getStringArray(headerId));  
  336.                 listViewTitle.setTitleCellValue(headerList);  
  337.             }  
  338.             return null;  
  339.         }  
  340.   
  341.         try {  
  342.             result = WSUtil.getSoapObjectByCallingWS(//这里是android调用webservice的典型方法  
  343.                     WebServiceConfig.NAMESPACE, methodName, params,  
  344.                     WebServiceConfig.wsdl);  
  345.         } catch (Exception e) {  
  346.             new AlertDialog.Builder(contextActivity)  
  347.                     .setTitle(R.string.message_title)  
  348.                     .setMessage(R.string.connection_error)  
  349.                     .setPositiveButton(R.string.OK_text, null).show();  
  350.             Log.e("Exception", e.getMessage());  
  351.         }  
  352.   
  353.         if (result == null) {//如果从webservice得到的结果为null,那我们就找到listview,并赋空值,将分页的显示值设为0  
  354.             ListView list = (ListView) contextActivity.findViewById(R.id.list);  
  355.             list.setAdapter(null);  
  356.             setTotalRecord(0);  
  357.             this.setCurrentPage(0);  
  358.             return null;  
  359.         }  
  360.   
  361.         SoapObject dataSet = WSObjectUtil.getDataSetObject(result);//如果从webservice得到的结果不为空,则转化成dataSet            
[java]  view plain copy print ?
  1. if (dataSet == null) {//如果dataSet为空,则显示对话框,没有查询到相关数据              
[java]  view plain copy print ?
  1.                             new AlertDialog.Builder(contextActivity)  
  2.             .setTitle(R.string.message_title).setMessage("没有查询到相关数据!")  
  3.             .setPositiveButton(R.string.OK_text, null);  
  4.   
  5.     return null;  
  6. }  
[java]  view plain copy print ?
  1. // 否则,就取得列名,并将取得的dataset转化成list型的数据,最后将数据显示在listview,并通过rows返回数据      
[java]  view plain copy print ?
  1.     List<String> columnNameList = Arrays.asList(contextActivity  
  2.             .getResources().getStringArray(headerNameId));  
  3.     WSObjectListUtil wsObjectListUtil = new WSObjectListUtil();  
  4.     if (contentTableName != null) {  
  5.         rows = wsObjectListUtil.getTableValueList(dataSet,  
  6.                 contentTableName, columnNameList);  
  7.     } else {  
  8.         rows = wsObjectListUtil.getTableValueList(dataSet, columnNameList);  
  9.     }  
  10.     if (wholeresult != null) {  
  11.         wholeresult.clear();  
  12.     }  
  13.     WSObjectMapUtil wsObjectMapUtil = new WSObjectMapUtil();  
  14.     if (contentTableName != null) {  
  15.         wholeresult = wsObjectMapUtil.getRowMapList(contentTableName,  
  16.                 dataSet);  
  17.     } else {  
  18.         wholeresult = WSObjectMapUtil.getRowMapList(dataSet);  
  19.     }  
  20.     SoapObject countTable = (SoapObject) dataSet.getProperty(dataSet  
  21.             .getPropertyCount() - 1);  
  22.     try {  
  23.         totalRecord = Integer  
  24.                 .parseInt(countTable.getProperty(0).toString());  
  25.     } catch (Exception e) {  
  26.         Log.e("Exception", e.getMessage());  
  27.     }  
  28.     if (totalRecord != 0 && this.totalRecord == 0  
  29.             && currentPageText != null) {  
  30.         this.setCurrentPage(1);  
  31.     }  
  32.     setTotalRecord(totalRecord);  
  33.   
  34.     if (bSetTitleValue == false) {  
  35.         bSetTitleValue = true;  
  36.         myListViewTitle listViewTitle = new myListViewTitle(contextActivity);  
  37.         if (this.columnWidths != null) {  
  38.             listViewTitle.setTitleCellWidth(this.columnWidths);  
  39.         }  
  40.         List<String> headerList = Arrays.asList(contextActivity  
  41.                 .getResources().getStringArray(headerId));  
  42.         listViewTitle.setTitleCellValue(headerList);  
  43.     }  
  44.   
  45.     ListView list = (ListView) contextActivity.findViewById(R.id.list);  
  46.     dataTableAdapter = new DataTableAdapter(contextActivity, rows);  
  47.     if (bMarkable) {  
  48.         dataTableAdapter.setMarkable(true);  
  49.     }  
  50.     if (this.columnWidths != null) {  
  51.         dataTableAdapter.setColumnWidths(this.columnWidths);  
  52.     }  
  53.     list.setAdapter(dataTableAdapter);//注意,关键点:将数据显示在listview  
  54.   
  55.     return rows;//返回数据  
  56. }  
好的,这个工具类基本就实现了。大家也可以自己添加一些元素完善。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值