Simple tutorial for Android-25th gun (Baas-based Data Table query pull-down refresh and pull-up loading implementation !), Androidbaas

Source: Internet
Author: User

Simple tutorial for Android-25th gun (Baas-based Data Table query pull-down refresh and pull-up loading implementation !), Androidbaas

In the previous section, we implemented data table loading. However, when there are a lot of data in the data table, we should consider the data paging. Here we chose the PullToRefreshListView control. Let's take a look at the description of this control:

:

Refreshing and refreshing


1. Import the Library

Download the source code (https://github.com/chrisbanes/Android-PullToRefresh), there is a Library project, add the project to Eclipse;

In addition, the extras folder has two projects: PullToRefreshListFragment and PullToRefreshViewPager. Because we cannot use their library files, we do not need to import them;

Ii. Practice 1. Create a project and add the Libray Library to the Project

After creating a project (try_PullToRefresh), right-click "Properties-" Android-"Add" and select the Library above.

2. Rewrite activity_main.xml

XML content:

[Html]View plaincopy
  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 = "vertical">
  6. <! -- The PullToRefreshListView replaces a standard ListView widget. -->
  7. <Com. handmark. pulltorefresh. library. PullToRefreshListView
  8. Android: id = "@ + id/pull_refresh_list"
  9. Android: layout_width = "fill_parent"
  10. Android: layout_height = "fill_parent"
  11. Android: cacheColorHint = "#00000000"
  12. Android: divider = "#19000000"
  13. Android: dividerHeight = "4dp"
  14. Android: fadingEdge = "none"
  15. Android: fastScrollEnabled = "false"
  16. Android: footerDividersEnabled = "false"
  17. Android: headerDividersEnabled = "false"
  18. Android: smoothScrollbar = "true"/>
  19. </LinearLayout>

Among them, the large segment <com. handmark. pull .................. /> Is equivalent to the ListView control. Use this section to replace the code of the original ListView control.

Next, let's take a look at how to implement it.

Insert data into the data table first:


Then read the code, MainActivity. java:

Package com. bmob. pagingdemo; import java. util. arrayList; import java. util. list; import android. app. activity; import android. content. context; import android. OS. bundle; import android. util. log; import android. view. layoutInflater; import android. view. view; import android. view. viewGroup; import android. widget. absListView; import android. widget. absListView. onScrollListener; import android. widget. baseAdapter; import android. widget. listView; import android. widget. textView; import android. widget. toast; import cn. bmob. v3.Bmob; import cn. bmob. v3.BmobQuery; import cn. bmob. v3.listener. findListener; import com. handmark. pulltorefresh. library. ILoadingLayout; import com. handmark. pulltorefresh. library. pullToRefreshBase; import com. handmark. pulltorefresh. library. pullToRefreshBase. onRefreshListener2; import com. handmark. pulltorefresh. library. pullToRefreshListView; public class MainActivity extends Activity {PullToRefreshListView mPullToRefreshView; private ILoadingLayout loadingLayout; ListView mMsgListView; List <TestData> bankCards = new ArrayList <TestData> (); // data listprivate static final int STATE_REFRESH = 0; // refresh the private static final int STATE_MORE = 1; // load more private int limit = 10; // The data on each page is 10 private int curPage = 0; // The number of the current page, starting from 0 @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Bmob. initialize (this, "initialize"); // initialize queryData (0, STATE_REFRESH); initListView (); // initialize ListView} private void initListView () {mPullToRefreshView = (PullToRefreshListView) findViewById (R. id. list); loadingLayout = mPullToRefreshView. getLoadingLayoutProxy (); loadingLayout. setLastUpdatedLabel (""); loadingLayout. setPullLabel (getString (R. string. pull_to_refresh_bottom_pull); // drop-down tag loadingLayout. setRefreshingLabel (getString (R. string. pull_to_refresh_bottom_refreshing); // refresh the tag loadingLayout. setReleaseLabel (getString (R. string. pull_to_refresh_bottom_release); // release the tag /// slide to listen to mPullToRefreshView. listener (new OnScrollListener () {@ Overridepublic void onScrollStateChanged (AbsListView view, int scrollState) {}@ Overridepublic void onScroll (AbsListView view, int cursor, int visibleItemCount, int totalItemCount) {if (firstVisibleItem = 0) {loadingLayout. setLastUpdatedLabel (""); loadingLayout. setPullLabel (getString (R. string. pull_to_refresh_top_pull); loadingLayout. setRefreshingLabel (getString (R. string. pull_to_refresh_top_refreshing); loadingLayout. setReleaseLabel (getString (R. string. pull_to_refresh_top_release);} else if (firstVisibleItem + visibleItemCount + 1 = totalItemCount) {// loadingLayout after loading. setLastUpdatedLabel (""); loadingLayout. setPullLabel (getString (R. string. pull_to_refresh_bottom_pull); loadingLayout. setRefreshingLabel (getString (R. string. pull_to_refresh_bottom_refreshing); loadingLayout. setReleaseLabel (getString (R. string. pull_to_refresh_bottom_release) ;}}); // pull-down refresh listener mPullToRefreshView. setOnRefreshListener (new OnRefreshListener2 <ListView> () {@ Overridepublic void trim (PullToRefreshBase <ListView> refreshView) {// pull-down refresh (loading data from the first page) queryData (0, STATE_REFRESH);} @ Overridepublic void onPullUpToRefresh (PullToRefreshBase <ListView> refreshView) {// pull up to load more (load next page data) queryData (curPage, STATE_MORE );}}); mMsgListView = mPullToRefreshView. getRefreshableView (); // set adaptermMsgListView. setAdapter (new DeviceListAdapter (this ));} /*** retrieve data by page ** @ param page * page number * @ param actionType * The operation type of ListView (pull-down refresh, pull-up, and load more) */private void queryData (final int page, final int actionType) {Log. I ("bmob", "pageN:" + page + "limit:" + limit + "actionType:" + actionType ); bmobQuery <TestData> query = new BmobQuery <TestData> (); query. setLimit (limit); // set the number of data queries per page. setSkip (page * limit); // query the number of data records. findObjects (this, new FindListener <TestData> () {@ Overridepublic void onSuccess (List <TestData> arg0) {// TODO Auto-generated method stubif (arg0.size ()> 0) {// load data to if (actionType = STATE_REFRESH) {// when the pull-down refresh operation is performed, reset the number of the current page to 0 and clear bankCards, re-add curPage = 0; bankCards. clear () ;}// Add the queried data to bankCards for (TestData td: arg0) {bankCards. add (td);} // after loading the data each time, add the current page number to 1. In this way, you do not need to operate curPage in the onPullUpToRefresh method refreshed on the previous page; showToast ("no." + (page + 1) + "page Data Loading completed");} else if (actionType = STATE_MORE) {// showToast ("no more data");} else if (actionType = STATE_REFRESH) {// No data showToast ("no data ");} mPullToRefreshView. onRefreshComplete () ;}@ Overridepublic void onError (int arg0, String arg1) {// TODO Auto-generated method stubshowToast ("query failed:" + arg1); mPullToRefreshView. onRefreshComplete () ;}}) ;}/ *** Adapter ** @ author Administrator **/private class DeviceListAdapter extends BaseAdapter {Context context; public DeviceListAdapter (Context context) {this. context = context ;}@ Overridepublic View getView (final int position, View convertView, ViewGroup parent) {ViewHolder holder = null; if (convertView = null) {convertView = LayoutInflater. from (context ). inflate (R. layout. list_item_bankcard, null); holder = new ViewHolder (); holder. TV _cardNumber = (TextView) convertView. findViewById (R. id. TV _cardNumber); convertView. setTag (holder);} else {holder = (ViewHolder) convertView. getTag () ;}testdata td = (TestData) getItem (position); holder. TV _cardNumber.setText (td. getName (); return convertView;} class ViewHolder {TextView TV _cardNumber;} @ Overridepublic int getCount () {return bankCards. size () ;}@ Overridepublic Object getItem (int position) {return bankCards. get (position) ;}@ Overridepublic long getItemId (int position) {return position ;}} private void showToast (String msg) {Toast. makeText (this, msg, Toast. LENGTH_SHORT ). show ();}}

TestData. java:

package com.bmob.pagingdemo;import cn.bmob.v3.BmobObject;public class TestData extends BmobObject {private String name;public String getName() {return name;}public void setName(String name) {this.name = name;}}
Main. xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.handmark.pulltorefresh.library.PullToRefreshListView        xmlns:ptr="http://schemas.android.com/apk/res/com.bmob.pagingdemo"        android:id="@+id/list"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:fadingEdge="none"        android:fastScrollEnabled="false"        android:smoothScrollbar="true"        ptr:ptrMode="both" /></LinearLayout>

AndroidManifest. xml:

<? Xml version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: android = "http://schemas.android.com/apk/res/android" package = "com. bmob. pagingdemo "android: versionCode =" 1 "android: versionName =" 1.0 "> <uses-sdk android: minSdkVersion =" 14 "android: targetSdkVersion = "14"/> <uses-permission android: name = "android. permission. INTERNET "/> <! -- Allow the application to open the network socket interface --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> <application android: allowBackup =" true "android: icon =" @ drawable/ic_launcher "android: label =" @ string/app_name "android: theme = "@ style/AppTheme"> <activity android: name = ". mainActivity "android: label =" @ string/app_name "> <intent-filter> <action android: name =" android. intent. action. MAIN "/> <category android: name =" android. intent. category. LAUNCHER "/> </intent-filter> </activity> </application> </manifest>


Run the instance, pull down and refresh:


Pull up and load 10 pieces of data each time:


This instance has a problem. data cannot be loaded when you enter the instance for the first time. You must drop down the instance to load the data. Please contact me: 291214603. Thank you!

If you like it, follow me! Thank you!


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.