dimanche 20 mars 2016

how to get rows count of SQLite in Android

I'm trying to get total records counts of Sq-lite database table when at the end of List View scrolling.But the major problem is first time getting count is accurate.When i again scrolling at the end count is increment. Suppose I have 181 total count of record , and again scroll at the end getting count is 362 , again scroll getting 543 ,...724, ...905 and etc. Why not get constant value like 181.Can someone help me to solve it .

Here is my Code for getting total count of records in Constant class

public final class Constant
{
    public static SQLiteDatabase db;
    public static MyDbHelper dbhelper;

   public static int GetLocalPages(Context context)
    {
        dbhelper = new MyDbHelper(context);
        dbhelper.onOpen(db);

        String query = "select count(*) from ALL_Post";
        db = dbhelper.getWritableDatabase();
        Cursor c = db.rawQuery(query, null);
        if (c.moveToFirst())
        {
            totalRowCount += c.getInt(0);
            Log.e("11111 ", " totalRowCount = " + totalRowCount);
        }
        if(totalRowCount < 50)
        {
            num= 1 ;
            Log.e("22222 "," num=  " +  num);
        }
        if(totalRowCount > 50)
        {
            num= totalRowCount / 50;
            Log.e("33333 ", " num=  " + num);
        }
        if(totalRowCount % 50> 0)
        {
            num += 1;
            Log.e("44444 ", " num =  " + num );
        }
        return num ;
    }

 }

And here is my listView scrollListener Method

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)
            {

                lastInScreen = firstVisibleItem + visibleItemCount;
                //Log.e("","lastInScreen="+lastInScreen);
                if(lastInScreen == totalItemCount)
                {
                    Toast.makeText(getApplicationContext(), "Your Last Item." + lastInScreen , Toast.LENGTH_SHORT).show();
int values= Constant.GetLocalPages(getApplicationContext());
                    Log.e("", "values=" + values);

                    }
            }
        });

Aucun commentaire:

Enregistrer un commentaire