Skip to content Skip to sidebar Skip to footer

Save List To Sqlite

I'm working on the Firebase app and I want to save a list of more than 5000 retrieved PricesList items into SQLite. Here is my Firebase code: @Override protected void onStart() {

Solution 1:

In your DBHelper you need a method that insert your data in db so.. first: Create the method

publicvoidisInsertData(Price price) {
   try {
      SQLiteDatabasedb=this.getWritableDatabase();
      ContentValuesinsertValues=newContentValues();
      insertValues.put(ItemCode, price.getItemCode());
      insertValues.put(Product, price.getProduct());
      db.insert(DB_NAME, null, insertValues);
    } catch (Exception e) {
      e.printStackTrace();
    }
}

I see that your table name looks similar to your database name, i recommend you change that but its if you want.

Second: we need a instance of our helper and call the new method, the next line goes in your iteration.

DbHelper dbHelper = new DbHelper(this); //or ActivityName.thisfor (DataSnapshot PricesListDataSnapshot : dataSnapshot.getChildren()) {
            PricesList pricesList = PricesListDataSnapshot.getValue(PricesList.class);
            pricesArrayList.add(pricesList);
            dbHelper.isInsertData(pricesList);
  }

That´s it! Now you save data in your database.

I recommend you read this link if you have any question after that https://developer.android.com/training/data-storage/sqlite

Solution 2:

I hardly recommend you to use an ORM of your preference before using pure SQLite, they are easier to understand, easy to configure and more scalable than using pure SQLite. This is the one that I have use the most "GreenDao: http://greenrobot.org/greendao/ ", but if you don't want or like this one you can use any other, there are many out there. An ORM maps your data objects into tables of SQLite and provides all the methods needed to manage your database. Inserting data using GreenDao ca be as easy as the following example:

NewsArticle[] newsArticles = new NewsArticle[600];
NewsArticle testArticle;
    for (int i = 0; i < 600; i++) {
         testArticle = newNewsArticle();
         testArticle.setTitle("title-text" + i);
         testArticle.setBody("body-text" + i);
         testArticle.setShortDescription("short-text" + i);
         testArticle.setDate(now);
         newsArticles[i] = testArticle;
    }
newsArticleDao.insertInTx(newsArticles);

Solution 3:

I'd like to say thanks to everyone who helped me and special thanks to @Daniel Carreto. I applied the steps and it works well, but I added a logical method (if) where (if Sqlite is Empty) call the firebase data and save it into SQLite then stop the calling of valueEventListener (if not) get the query of SQLite.class. it works well but when i finish the activity and open it again the process starts again here is the code after update

@OverrideprotectedvoidonStart() {
    super.onStart();

    if (FB2sqLite == null) {

        ArrayList<HashMap<String, String>> myList = FB2sqLite.getProducts();
        if (myList.size() != 0) {
            ListAdapter adapter = newSimpleAdapter(FireBase_Prices.this, myList, R.layout.model,
                    newString[]{item_Code, ItemDesc},
                    new int[]{R.id.ItemCodeView, R.id.descTxt});
            listView.setAdapter(adapter);
        }

    } else {

        mRef.addValueEventListener(newValueEventListener() {
            @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {

                pricesArrayList.clear();

                for (DataSnapshotPricesListDataSnapshot : dataSnapshot.getChildren()) {

                    PricesList pricesList = PricesListDataSnapshot.getValue(PricesList.class);
                    pricesArrayList.add(pricesList);
                    FB2sqLite.insert(pricesList);
                    FB2sqLite.close();
                }

                ArrayList<HashMap<String, String>> myList = FB2sqLite.getProducts();
                if (myList.size() != 0) {
                    ListAdapter adapter = newSimpleAdapter(FireBase_Prices.this, myList, R.layout.model,
                            newString[]{item_Code, ItemDesc},
                            new int[]{R.id.ItemCodeView, R.id.descTxt});
                    listView.setAdapter(adapter);
                }
            }

            @OverridepublicvoidonCancelled(DatabaseError databaseError) {

            }
        });
    }
}

how can i use removeEventListener and where is the position of this method?

Post a Comment for "Save List To Sqlite"