samedi 10 octobre 2015

Load the next 10 rows on scroll end of listView

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://ift.tt/nIICcg"
    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!

Aucun commentaire:

Enregistrer un commentaire