Android implementation based on sliding SQLite data paging loading technology (with demo source download) _android

Source: Internet
Author: User
Tags sqlite sqlite database


This article describes the Android implementation based on sliding SQLite data paging loading technology. Share to everyone for your reference, specific as follows:



Main.xml is as follows:


<menu xmlns:android= "Http://schemas.android.com/apk/res/android" >
 <item
 android:id= "@+id/" Action_settings "
 android:orderincategory=" "
 android:showasaction=" never "
 android:title=" @string /action_settings "/>
</menu>


Mainactivity.java is as follows:


package com.example.testscrollsqlite;
import java.util.ArrayList;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnScrollListener {
 private TextView loadInfo;
 private ListView listView;
 private LinearLayout loadLayout;
 private ArrayList <String> items;
 private DatabaseService service;
 private int currentPage = 1; // default is on the first page
 private static final int lineSize = 7;
 private int allRecorders = 0; // number of all records
 private int pageSize = 1; // default one page
 private int lastItem;
 private Aleph0 baseAdapter;
 @Override
 protected void onCreate (Bundle savedInstanceState) {
  super.onCreate (savedInstanceState);
  setContentView (R.layout.activity_main);
  listView = (ListView) findViewById (R.id.listview);
  // Create a subscript linear layout to display "Loading"
  loadLayout = new LinearLayout (this);
  loadLayout.setGravity (Gravity.CENTER);
  // Define a text to display "Loading"
  loadInfo = new TextView (this);
  loadInfo.setText ("Loading ...");
  loadInfo.setGravity (Gravity.CENTER);
  // Add components
  loadLayout.addView (loadInfo, new LayoutParams (
    LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  // Add to the bottom of listView
  listView.addFooterView (loadLayout);
  listView.setOnScrollListener (this);
  showAllData ();
 }
 / **
  * Read all data
  * /
 public void showAllData () {
  service = new DatabaseService (this);
  allRecorders = service.getCount ();
  // Calculate the total number of pages
  pageSize = (allRecorders + lineSize -1) / lineSize;
  System.out.println ("allRecorders =" + allRecorders);
  System.out.println ("pageSize =" + pageSize);
  items = service.getAllItems (currentPage, lineSize);
  for (int i = 0; i <items.size (); i ++) {
   System.out.println (items.get (i));
  }
  baseAdapter = new Aleph0 ();
  listView.setAdapter (baseAdapter);
 }
 @Override
 public void onScroll (AbsListView view, int firstVisible, int visibleCount,
   int totalCount) {
  lastItem = firstVisible + visibleCount-1; // Status whether to the end
 }
 @Override
 public void onScrollStateChanged (AbsListView view, int scorllState) {
  System.out.println ("Enter the scrolling interface");
  // Whether it reaches the bottom and the data has not been read
  if (lastItem == baseAdapter.getCount ()
    && currentPage <pageSize // No more scrolling
    && scorllState == OnScrollListener.SCROLL_STATE_IDLE) {
   currentPage ++;
   // Set the display position
   listView.setSelection (lastItem);
   // Add data
   appendDate ();
  }
 }
 / **
  * Increase data
  * /
 private void appendDate () {
  ArrayList <String> additems = service.getAllItems (currentPage, lineSize);
  baseAdapter.setCount (baseAdapter.getCount () + additems.size ());
  // Judge, remove "Loading" if it reaches the end
  if (allRecorders == baseAdapter.getCount ()) {
   listView.removeFooterView (loadLayout);
  }
  items.addAll (additems);
  // Notify record change
  baseAdapter.notifyDataSetChanged ();
 }
 class Aleph0 extends BaseAdapter {
  int count = lineSize; / * starting amount * /
  public int getCount () {
   return count;
  }
  public void setCount (int count) {
   this.count = count;
  }
  public Object getItem (int pos) {
   return pos;
  }
  public long getItemId (int pos) {
   return pos;
  }
  public View getView (int pos, View v, ViewGroup p) {
   TextView view = new TextView (MainActivity.this);
   view.setTextSize (60);
   if (items! = null) {
    view.setText (items.get (pos));
   } else {
    view.setText (pos);
   }
   return view;
  }
 }
} 

Full instance code click here to download the site.



More interested readers of Android-related content can view this site: "Android operation SQLite Database Skills Summary", "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", " The activity Operation skill summary of Android programming, the "Android file Operation skill Summary", "Android programming development SD Card Operation method Summary", "Android Development introduction and Advanced Course", "Android Resource Operation skill Summary", " Android View tips Summary and a summary of the use of Android controls



I hope this article will help you with the Android program.


Related Article

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.