Android listview scroll sliding screen shows sqlite paging, similar to chat records, listview dynamically adds sqlite paging data

Source: Internet
Author: User

Android listview scroll sliding screen shows sqlite paging, similar to chat records, listview dynamically adds sqlite paging data
I recently used openfire-based chat (like QQ) to view chat records on pages. For this reason, I used to view chat records in reverse chronological order, declare that the demo is in reverse order based on the id (the principle is the same as the time in reverse order) 1. MainActivity on the main interface. class

Public class MainActivity extends Activity implements OnScrollListener {private ProgressBar loadInfo; private ListView listView; private LinearLayout loadLayout; private ArrayList
 
  
Items; private DatabaseService service; private int currentPage = 1; // default value: private static final int lineSize = 10 on the first page; // The number of private int allRecorders = 0 displayed each time; // Number of all records private int pageSize = 1; // by default, a page of private Aleph0 baseAdapter is displayed; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); listView = (ListView) findViewById (R. id. listview); service = new DatabaseService (this); // create a badge layout to display loadLayout = new LinearLayout (this); loadLayout. setGravity (Gravity. CENTER); // define a ProgressBar to indicate "loading" loadInfo = new ProgressBar (this, null, android. r. attr. progressBarStyleSmall); // adds the loadLayout component. addView (loadInfo, new LayoutParams (LinearLayout. layoutParams. MATCH_PARENT, LinearLayout. layoutParams. WRAP_CONTENT); // Add it to the listView header listView. addHeaderView (loadLayout); listView. setOnScrollListener (this); showAllData ();}/*** read all data */public void showAllData () {allRecorders = service. getCount (); // calculate the total number of pages pageSize = (allRecorders + lineSize-1)/lineSize; items = service. getAllItems (currentPage, lineSize); Collections. reverse (items); // reverse baseAdapter = new Aleph0 (); listView. setAdapter (baseAdapter); listView. setSelection (items. size (); // locate to the bottom} int firstItem =-1; @ Overridepublic void onScroll (AbsListView absView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {firstItem = firstVisibleItem;} @ Overridepublic void onScrollStateChanged (AbsListView view, int scorllState) {if (firstItem = 0 & currentPage <pageSize & scorllState = OnScrollListener. SCROLL_STATE_IDLE) {// no longer scroll logs. e (log, slide to the first place); currentPage ++; // Add data appendDate () ;}}/*** add data */private void appendDate () {final ArrayList
  
   
Additems = service. getAllItems (currentPage, lineSize); Collections. reverse (additems); baseAdapter. setCount (baseAdapter. getCount () + additems. size (); // judge. if it reaches the end, remove "loading" if (allRecorders = baseAdapter. getCount () {listView. removeHeaderView (loadLayout);} items. addAll (0, additems); baseAdapter. yydatasetchanged (); listView. setSelection (additems. size ();} class Aleph0 extends BaseAdapter {int count = line Size; 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 ;}}}
  
 
2. layout file activity_main.xml on the main interface
     
  
 
3. Database Operations
Public class DatabaseService {private Context mContext; private MyDBOpenHelper dbHelper; public DatabaseService (Context context) {mContext = context; dbHelper = new MyDBOpenHelper (mContext );} // Add public void insert (String title) {SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = insert into database (title) values (?); Db.exe cSQL (SQL, new String [] {title});} // delete public void delete (String title) {SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = delete from database where title = ?; Db.exe cSQL (SQL, new String [] {title});} // SEARCH FOR public ArrayList
 
  
Find (int id) {SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = select * from database where _ id =? ; Cursor c = db. rawQuery (SQL, new String [] {String. valueOf (id)}); ArrayList
  
   
Titles = new ArrayList
   
    
(); If (c. moveToNext () {String title = c. getString (c. getColumnIndexOrThrow (MyDBOpenHelper. FIELD_TITLE); titles. add (title); return titles;} // do not forget to close Cursor. C. close (); return null;} // upDate public void upDate (int id, String title) {SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = update database set title =? Where _ id = ?; Db.exe cSQL (SQL, new String [] {String. valueOf (title), String. valueOf (id)});} // total number of query records public int getCount () {SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = select count (*) from database; Cursor c = db. rawQuery (SQL, null); c. moveToFirst (); int length = c. getInt (0); c. close (); return length ;} /***** paging query ** @ param currentPage current page * @ param pageSize records displayed on each page * @ return records on the current page */public ArrayList
    
     
GetAllItems (int currentPage, int pageSize) {int firstResult = (currentPage-1) * pageSize; int maxResult = currentPage * pageSize; SQLiteDatabase db = dbHelper. getWritableDatabase (); String SQL = select * from database order by _ id desc limit ?,? ; Cursor mCursor = db. rawQuery (SQL, new String [] {String. valueOf (firstResult), String. valueOf (pageSize)}); ArrayList
     
      
Items = new ArrayList
      
        (); Int columnCount = mCursor. getColumnCount (); while (mCursor. moveToNext () {String item = mCursor. getString (mCursor. getColumnIndexOrThrow (MyDBOpenHelper. FIELD_TITLE); items. add (item) ;}// do not close the database return items ;}}
      
     
    
   
  
 
4,

 

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.