//------------------------------------------------------------------------// //UI operation: we need to find a neat way, as to do this operation async and tie this with UI thread later on async void dbupdateUI(scrolloptions sclopt) { if( null == mItemList) mItemList = new ObservableCollection<ShopItem>(); //create the db helper class var dbhelper = new DBHelper(DBGlobal.DatabasebFilePath, this); //create or open shopitem database var result = dbhelper.create_database(); if (!result) { Toast.MakeText(this, "Failed to open / create the database ", ToastLength.Long).Show(); return; } var db_list = dbhelper.query_selected_values("select * from ShopItem"); //if there were no entries then we might hv got a null, in that case, take them to add a new entry if (db_list != null) { mItemList.Clear(); foreach (var shopping_item in db_list) { mItemList.Add(shopping_item); } } if( mfiltered_list.Count > 0 ) { mfiltered_list.Clear(); foreach(var newitem in mItemList) { mfiltered_list.Add(newitem); } } m_adapter.NotifyDataSetChanged(); switch (sclopt) { case scrolloptions.scroll_to_bottom: await Task.Delay(1000); mListview.SmoothScrollToPosition(m_adapter.Count - 1); break; case scrolloptions.scroll_to_top: await Task.Delay(1000); mListview.SmoothScrollToPosition(0); break; default: case scrolloptions.scroll_none: break; } }
private void HLayot_Click(object sender, EventArgs e) { var layout = sender as LinearLayout; Console.WriteLine("selected:{0}", layout.Tag); var builder = new AlertDialog.Builder(this) .SetTitle("Shopping Completed") .SetMessage("Have you purchased this item already??") .SetNegativeButton("No", (EventHandler<DialogClickEventArgs>)null) .SetPositiveButton("Yes", (EventHandler<DialogClickEventArgs>)null); var dialog = builder.Create(); dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; dialog.Show(); // Get the buttons. var yesBtn = dialog.GetButton((int)DialogButtonType.Positive); var noBtn = dialog.GetButton((int)DialogButtonType.Negative); // Assign our handlers. yesBtn.Click += delegate { //create the db helper class var dbhelper = new DBHelper(DBGlobal.DatabasebFilePath, this); //create or open shopitem database var result = dbhelper.create_database(); double cost = dbhelper.delete_rows(Int32.Parse(layout.Tag.ToString())); refreshUI(); reduce_budget((int)cost); dialog.Dismiss(); }; noBtn.Click += delegate { // Dismiss dialog. Console.WriteLine("I will dismiss now!"); refreshUI(); dialog.Dismiss(); }; }
//------------------------------------------------------------------------// //bundled all the delete options together private void delete_item(deleteoptions dopt,int ID) { string represent_items = ""; List<int> ids_to_delete = new List<int>(); try { switch (dopt) { case deleteoptions.delete_this_item: represent_items = " this item "; ids_to_delete.Add(ID); break; case deleteoptions.delete_selected_items: represent_items = " those selected items "; if (0 == m_queue_for_deletion.Count) { Toast.MakeText(this, "No items were selected", ToastLength.Long).Show(); return; } foreach(var kvc in m_queue_for_deletion) { if( kvc.Value == true) ids_to_delete.Add(kvc.Key); } break; case deleteoptions.delete_all_items: represent_items = " all your items "; if (0 == mItemList.Count) { Toast.MakeText(this, "No items are present", ToastLength.Long).Show(); return; } foreach (var i in mItemList) ids_to_delete.Add(i.ID); break; default: break; } m_queue_for_deletion.Clear(); var builder = new AlertDialog.Builder(this) .SetTitle("Action Required") .SetMessage("Do you want to remove" + represent_items + "from the list?") .SetNegativeButton("No", (EventHandler<DialogClickEventArgs>)null) .SetPositiveButton("Yes", (EventHandler<DialogClickEventArgs>)null); var dialog = builder.Create(); dialog.Window.Attributes.WindowAnimations = Resource.Style.dialog_animation; dialog.Show(); // Get the buttons. var yesBtn = dialog.GetButton((int)DialogButtonType.Positive); var noBtn = dialog.GetButton((int)DialogButtonType.Negative); // Assign our handlers. yesBtn.Click += delegate { //create the db helper class var dbhelper = new DBHelper(DBGlobal.DatabasebFilePath, this); //create or open shopitem database var result = dbhelper.create_database(); if (!result) { Toast.MakeText(this, "Failed to open / create the database ", ToastLength.Long).Show(); return; } foreach (var item_id in ids_to_delete) { dbhelper.delete_rows(item_id); } dbupdateUI(scrolloptions.scroll_to_top); dialog.Dismiss(); }; noBtn.Click += delegate { // Dismiss dialog. Console.WriteLine("I will dismiss now!"); m_adapter.NotifyDataSetChanged(); dialog.Dismiss(); }; } catch(Exception ex) { Console.WriteLine("ExceptioHandled:{0}" + ex.Message); } }
private List<ShopItem> get_all_shopitems() { List<ShopItem> shop_items = new List<ShopItem>(); //create the db helper class var dbhelper = new DBHelper(DBGlobal.DatabasebFilePath,this); //create or open shopitem database var result = dbhelper.create_database(); var db_list = dbhelper.query_selected_values("select * from ShopItem"); //if there were no entries then we might hv got a null, in that case, take them to add a new entry if (db_list != null) { shop_items.Clear(); foreach (var shopping_item in db_list) { shop_items.Add(shopping_item); } } return shop_items; }