Tuesday, 6 November 2018

How to display a Very large amount of data from SQLite DB to your Grid /List

This is a post for how to display a large amount of data i.e 30k + product records and near about 80 tables in DB.. such applications are for large company or a large/ very large ERP or CRM system and may be for end to end users and may not available on Play store.. Anyways simply this piece of code will help you to fetch the data and display them in very easy manner and your DB will net get load even if a large DB you have, and your APP will not have to wait for loading time :) 

Step 1 we have to use synchronized DB if you have to open and close DB frequently and multiple function i.e insert update and delete at same time on DB


public class DataBaseHelper extends SQLiteOpenHelper {

 public static synchronized DataBaseHelper getInstance(Context context) {
   private static DataBaseHelper mInstance;//Singleton Instance
        if (mInstance == null) {
            mInstance = new DataBaseHelper(context);
        }
        return mInstance;
    }

    public static synchronized DataBaseHelper getHelper(Context context) {
        if (mInstance == null) {
            mInstance = new DataBaseHelper(context);
        }
        REDABLE_DATABASE = mInstance.getReadableDatabase();
        WRITABLE_DATABASE = mInstance.getWritableDatabase();

        return mInstance;
    }
   
   
}


Here i am displaying a large data in to Grid... and i have a criteria

  1.  Display data i.e product Name,Product Price, Product Discount price ,Discounter Percentage ,No of product Orders and lots more.. (around 28 possibilities) and that depends upon Admin panel so layout is also not static
  2. Grid cell have swipe feature so user can see some extra info when they swipe on grid cell and also it depends upon Admin panel so layout is also not static

So it can say a large requirement for tablet users...who not want to wait for getting/fetching data from the application And we not fetching data each time from API but at app start and when user select for the Re-sync the app/ at time that fixed from admin panel after that time user need to forcefully re sync the APP if internet available else whenever get connectivity for internet... in other cases we display data from local DB. and user must not know that from this big amount of data he is getting only 30/50 records each time still they have option of sorting alphabetically,date of created, product availability and lots more (can say around 15 different sorting possibility ) or by it's attributes like only black color product only abc type of products and lots more

So to get data to display in grid/List or recycle-view we've to get all the IDs of product... as i have some filtering process to do on product i.e display on Black color product, only product that is 10 inches long etc so my first requirement on product table to filter only those records if user have some select any criteria  to see the product else display all the products around 30,000+ product and increasing on each re-sync process :) but below code useful for achieving what we want :) :)
So lets get started...

public static int NOOFPRODUCTSIN1PAGE =30;//50 is my global 
variable and i maintain each scroll
how many record fetch from DB
public class ProductGrid_Fragment extends android.support.v4.app.Fragment {
Map<Integer, String> IDs_HashMap = new HashMap<>();
//below is logic for making small group of
  class makeSmallGroupsOfID extends AsyncTask<Void, Void, Void> {
        ArrayList<String> myList;
        @Override
        protected Void doInBackground(Void... params) {
            // System.out.println("THIS IS makeSmallGroupsOfID DO IN BG");
            if (listproductIds.size() > DB.NOOFPRODUCTSIN1PAGE) {
                int size = listproductIds.size();
                float noofarray = (float) size / DB.NOOFPRODUCTSIN1PAGE;
                //    System.out.println(noofarray + " noofarray **");
                if (noofarray == Math.round(noofarray)) {
               
                } else {
                  
                    noofarray = noofarray + 1;//Not an integer and have to add 1 more to array :)
                }
                System.out.println((int) noofarray + " ***** ");
                int arry = (int) noofarray;
                int start = 0, end = DB.NOOFPRODUCTSIN1PAGE;
                for (int iarray = 1; iarray <= arry; iarray++) {
                    String s;              
                 s = listproductIds.subList(start, end).toString();
                    s = s.replaceAll("]", "");
                    s = s.replace("[", "");
                    s = s.replaceAll(" ", "");
                    IDs_HashMap.put(iarray, s + "");
                    if (iarray == arry - 1) {
                        start = end;
                        end = listproductIds.size();
                    } else {
                        start = end;
                        end = end + DB.NOOFPRODUCTSIN1PAGE;
                    }
                }
                myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(1).split(",")));
                //   ArrayList<String> tempIDM=    IDs.get(0).toString();

            } else {
                String s = listproductIds.subList(0, listproductIds.size()).toString();
                System.out.println(listproductIds.toString() + " ***** ");
             
                s = s.replaceAll("]", "");
                s = s.replace("[", "");
                s = s.replaceAll(" ", "");
                IDs_HashMap.put(1, s + "");
                myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(1).split(",")));

            }
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            getProduct(myList);
            super.onPostExecute(aVoid);
        }
    }
And below is my method that called on each scroll of grid/List or recycle-view :)
 gridproductitem.setOnScrollListener(new EndlessScrollListener() {
            @Override
            public boolean onLoadMore(int page, int totalItemsCount) {

           
                if (IDs_HashMap != null && page <= IDs_HashMap.size()) {
                    ArrayList<String> myList = new ArrayList<>(Arrays.asList(IDs_HashMap.get(page).split(",")));
              
                    String prodid = myList.toString().replace("[", "").replace("]", "").replace(",", "','").replace(" ", "");

               

                    updateGrid.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);//this function will notify my adapter
                }
                return true;
            }
        });
       
        }
        If found any query in implementation or other code related query write me a mail will happy to help :)
        In my feature post you can find location fetching in to background used for fetching current location of device... A very well known company of Gujarat it self using it :)