mercredi 18 février 2015

Android List to Database

I have a custom List which holds information stored from a online mysql database. I now want to put this List into a sqlite internal database. The table has already been created in the database. I also have a databasehelper class which is working fine.


All the list information is stored in FoodInfoModel class which is made of get and set properties.


Do I create a method in the databasehelper class to insert the whole list at once? not sure how to go about it.


Current Method in databasehelper



public void addDiet(FoodInfoModel foodinfomodel) {


SQLiteDatabase db = getReadableDatabase();

ContentValues values = new ContentValues();
values.put(KEY_DIET_ID, foodinfomodel.getDietID());
values.put(KEY_DAY, foodinfomodel.getDay());
values.put(KEY_QTY, foodinfomodel.getQty());
values.put(KEY_TIME_FOOD, foodinfomodel.getTime());
values.put(KEY_ITEM_FOOD, foodinfomodel.getItem());
values.put(KEY_MEASURE, foodinfomodel.getMeasure());

// Inserting Row
db.insert("my_diet", null, values);
db.close(); //
}


Function to set List and Adapter



public void onFetchComplete(List<FoodInfoModel> data) {

this.data = data;

System.out.println("data is " + data);

if(dialog != null) dialog.dismiss();

// create new adapter
adapter = new DietAdapterNew(this, data);
// set the adapter to list
setListViewHeightBasedOnChildren(listview);
listview.setAdapter(adapter);
adapter.notifyDataSetChanged();


}


How do i add that data list to the internal sqlite db?


Thanks!


Aucun commentaire:

Enregistrer un commentaire