0

lets say i have 500 data on my sqlite database and to make it easy to load the data i load it from 1 to 10 and 10 to 20 and so on every time the end of listview is reach,

here is my code
on my Main xml i have

<ListView
        android:id="@+id/list_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
/>

and my custom list item

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp" >
    <TextView
       android:id="@+id/KeyWord"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/thumbnail"
        android:layout_toRightOf="@+id/thumbnail"
        android:text="@string/sample"
        android:textColor="@color/grey_blue"
        android:typeface="serif"
        android:textSize="18sp"
        android:textStyle="bold"
        android:shadowColor="@color/red_minus"
        android:layout_marginTop="5dp"/>

    <TextView
        android:id="@+id/thisSub"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/thisWord"
        android:layout_below="@+id/thisWord"
        android:text="@string/sample"
        android:textColor="#343434"
        android:typeface="serif"
        android:textSize="16sp" />
</RelativeLayout>

and here how i populate the first 10 rows to my listView on my MainActivity

private void displayListView(final String SpinnerData) {
        Cursor cursor = null;
        try {
            cursor = dbHelper.fetchAllData(SpinnerData);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), "EER " + e, Toast.LENGTH_LONG).show();
        }
        String[] columns = new String[]{
                DbAdapter.KEY_WORD, DbAdapter.KEY_PRO
        };
        int[] to = new int[]{
                R.id.KeyWord, R.id.thisSub
        };
        dataAdapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.list_item, cursor, columns, to, 0);
        final ListView listView = (ListView) findViewById(R.id.list_view);
        listView.setAdapter(dataAdapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }
            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                final int lastItem = firstVisibleItem + visibleItemCount;
                if(lastItem == totalItemCount){
                   // here i want to add the next 10 rows on scrollend
                }
            }
        });
    }

using my dbHelper on my DbAdapter class

public Cursor fetchAllData(String baseFrom){                                    
   Cursor mCursor = null;                                                        
    if(baseFrom == "ALL"){                                                       
        mCursor = mDb.query(true,SQLITE_TABLE, new String[]{                     
                KEY_ROWID, KEY_PRO, KEY_WORD},         
                null, null, null, null, KEY_WORD, "10");                         
    }else{                                                                       
        mCursor = mDb.query(true,SQLITE_TABLE, new String[]{                     
                KEY_ROWID, KEY_PRO, KEY_WORD},         
                KEY_FROM + " LIKE '" + baseFrom + "%'",                          
                null, null, null, KEY_WORD, "10");                               
    }                                                                                                                           
    if(mCursor != null){                                                         
        mCursor.moveToFirst();                                                   
        if (!(mCursor.moveToFirst()) || mCursor.getCount() == 0){                
            mCursor = null;                                                      
        }                                                                        
    }return mCursor;                                                             
}        

my problem is, i dont know how can i add the next 10 rows at the end of my listview on scrollend, im just a newbie and this is part of my self learning.
i was thinking to get the next 10 rows by using the LIMIT like.

        mCursor = mDb.query(true,SQLITE_TABLE, new String[]{                     
        KEY_ROWID, KEY_PRO, KEY_WORD},         
        KEY_FROM + " LIKE '" + baseFrom + "%'",                          
        null, null, null, KEY_WORD, "10, 20"); //Here from 10 to 20 

and append it at the end of my listview but i dont know can i code it.
anyone can help me to do it?
i will appreciate anyone's help. Thank you very much in advance!

bernzkie
  • 1,269
  • 2
  • 16
  • 35
  • What you could do is once you know that a user has scrolled to the end of the current list, load more data to the arraylist and automatically smoothScrollToPosition(int) - this position should be tracked in your code so that you take the user to where the new data begin/start. – Eenvincible Oct 10 '15 at 15:57
  • any code for this? Thank you!!! – bernzkie Oct 10 '15 at 15:58
  • Look at this link: http://stackoverflow.com/questions/26451888/sqlite-get-x-rows-then-next-x-rows – Skywalker Oct 10 '15 at 16:00
  • this actually where i get the idea to use LIMIT to load 10 to 20 and so on... but i don't know how to implement it to my code. i'm just a newbie. :- – bernzkie Oct 10 '15 at 16:03
  • why at all do you want to do that? i have a table with 250,000 rows and with no "pagination" it just works – pskink Oct 10 '15 at 16:34
  • i just trying learn on how can i handle a large data with scrollend in listView, maybe there's any solution for handling large data for displaying it into listView but i want to make it simple with scrollend. – bernzkie Oct 11 '15 at 01:26
  • Your main problem is not the Listview, it is with querying the database. You have to figure out how to get data on the first 10, second 10, and so on. That means you have to use a SORT order, which I don't see from your code. – The Original Android Oct 11 '15 at 02:38
  • I just noticed the sample link you were using. Keyword "limit" would not work for you. – The Original Android Oct 11 '15 at 02:48
  • you don't need any "LIMIT" keywords, yo have 500 rows i have 250,000 that is 500 x more and it just works with no "LIMIT" keywords – pskink Oct 11 '15 at 06:34
  • lets say i only have 500 data, but WHAT IF i have more than 500, or 250000 data on my database, i want to handle the large data, it doesn't matter how much data i have or you have, what i want is to handle the data with scrollend. i want it to load the first 10 rows, even i only have 20 data and at scrollend of listview the next 10 row which will start from 11 to 20 will be loaded to at SCROLL END. – bernzkie Oct 11 '15 at 23:23

1 Answers1

0

As I said in my comment, the main problem is getting data by groups of 10 items at a time. In order to do that, you may use a SORTing order. Here is a documentation on it @ SQLiteDatabase Cursor query . Look at parameter orderBy in this case.

The Original Android
  • 6,147
  • 3
  • 26
  • 31