Sreedevi v

Sreedevi v

  • NA
  • 166
  • 5.2k

Save Edit Update Xamarin

Mar 13 2019 11:20 PM
I have 2 pages one is master other detail page.
 
detailpage.xaml
  1. <ContentPage.ToolbarItems>  
  2. <ToolbarItem Icon="plus.png" Text="Add" Activated="OnAdd" Order="Primary"/>  
  3. </ContentPage.ToolbarItems>  
  4. <ListView x:Name="iList" ItemSelected="IList_ItemSelected" >  
  5. <ListView.ItemTemplate>  
  6. <DataTemplate>  
  7. <TextCell Text="{Binding ExpenseCategory}" Detail="{Binding ExId}" >  
  8. <TextCell.ContextActions>  
  9. <MenuItem Text="Delete"  
  10. IsDestructive="True"  
  11. Clicked="MenuItem_Clicked"  
  12. CommandParameter="{Binding .}" />  
  13. </TextCell.ContextActions>  
  14. </TextCell>  
  15. </DataTemplate>  
  16. </ListView.ItemTemplate>  
  17. </ListView>  
detailpage.xaml.cs
  1. public event EventHandler<Expense> ExpenseAdded;  
  2. public event EventHandler<Expense> ExpenseUpdated;  
  3. private SQLiteAsyncConnection _connection;  
  4. public ExpenseDetail (Expense addexpense)  
  5. {  
  6. if (addexpense == null)  
  7. throw new ArgumentNullException(nameof(addexpense));  
  8. InitializeComponent();  
  9. _connection = DependencyService.Get<ISQLiteDb>().GetConnection();  
  10. BindingContext = new Expense  
  11. {  
  12. ExId = addexpense.ExId,  
  13. ExpenseCategory = addexpense.ExpenseCategory,  
  14. Amount=addexpense.Amount,  
  15. ItemName=addexpense.ItemName,  
  16. Exdate=addexpense.Exdate  
  17. };  
  18. }  
  19. protected async override void OnAppearing()  
  20. {  
  21. var pick = await _connection.Table<ExpenseCatMaster>().ToListAsync();  
  22. expenseList.ItemsSource = pick;  
  23. expenseList.ItemDisplayBinding = new Binding("ExCategory");  
  24. base.OnAppearing();  
  25. }  
  26. private async void OnSave(object sender, EventArgs e)  
  27. {  
  28. var expense = new Expense  
  29. {  
  30. ExpenseCategory = expenseList.Items[expenseList.SelectedIndex].ToString(),  
  31. SelectedIndex = expenseList.SelectedIndex,  
  32. // SubCategory= subList.Items[subList.SelectedIndex].ToString(),  
  33. ItemName=itemName.Text,  
  34. Amount = Convert.ToDecimal(amount.Text),  
  35. Exdate=datepick.Date  
  36. };  
  37. await _connection.InsertAsync(expense);  
  38. await Navigation.PopAsync();  
  39. }  
after saving the detils .. it will appear in master page
 
when i click the iteam we want to change ie,In edit mode , picker doesnot display the selected item
 
and now when i click save button i ahve to update existing data . Dont create new fields in table.
 
masterpage.xaml
  1. <ContentPage.ToolbarItems>  
  2. <ToolbarItem Icon="plus.png" Text="Add" Activated="ToolbarItem_Activated" Order="Primary"/>  
  3. </ContentPage.ToolbarItems>  
  4. <ListView x:Name="iList" ItemSelected="ListView_ItemSelected">  
  5. <ListView.ItemTemplate>  
  6. <DataTemplate>  
  7. <TextCell Text="{Binding Amount}" Detail="{Binding InId}" >  
  8. <TextCell.ContextActions>  
  9. <MenuItem Text="Delete"  
  10. IsDestructive="True"  
  11. Clicked="MenuItem_Clicked"  
  12. CommandParameter="{Binding .}" />  
  13. </TextCell.ContextActions>  
  14. </TextCell>  
  15. </DataTemplate>  
  16. </ListView.ItemTemplate>  
  17. </ListView>  
masterpage.xaml.cs
  1. public partial class AddExpense : ContentPage  
  2. {  
  3. private ObservableCollection<Expense> _income;  
  4. private SQLiteAsyncConnection _connection;  
  5. private bool _isDataLoaded;  
  6. public AddExpense ()  
  7. {  
  8. InitializeComponent ();  
  9. _connection = DependencyService.Get<ISQLiteDb>().GetConnection();  
  10. }  
  11. protected async override void OnAppearing()  
  12. {  
  13. if (_isDataLoaded)  
  14. return;  
  15. _isDataLoaded = true;  
  16. await LoadData();  
  17. base.OnAppearing();  
  18. }  
  19. private async Task LoadData()  
  20. {  
  21. await _connection.CreateTableAsync<Expense>();  
  22. var incomeTab = await _connection.Table<Expense>().ToListAsync();  
  23. _income = new ObservableCollection<Expense>(incomeTab);  
  24. iList.ItemsSource = _income;  
  25. }  
  26. private async void OnAdd(object sender, EventArgs e)  
  27. {  
  28. var page = new ExpenseDetail(new Expense());  
  29. page.ExpenseAdded += (source, expense) =>  
  30. {  
  31. _income.Add(expense);  
  32. };  
  33. await Navigation.PushAsync(page);  
  34. }  
  35. private async void IList_ItemSelected(object sender, SelectedItemChangedEventArgs e)  
  36. {  
  37. if (iList.SelectedItem == null)  
  38. return;  
  39. var selectIncome = e.SelectedItem as Expense;  
  40. iList.SelectedItem = null;  
  41. var page = new ExpenseDetail(selectIncome);  
  42. //page.ExpenseUpdated += (source, expense) =>  
  43. //{  
  44. // selectIncome.ExId = expense.ExId;  
  45. // selectIncome.ExpenseCategory = expense.ExpenseCategory;  
  46. // selectIncome.ItemName = expense.ItemName;  
  47. // selectIncome.Exdate = expense.Exdate;  
  48. // selectIncome.Amount = expense.Amount;  
  49. //};  
  50. await Navigation.PushAsync(page);  
  51. }  
  52. private async void MenuItem_Clicked(object sender, EventArgs e)  
  53. {  
  54. var income = (sender as MenuItem).CommandParameter as Expense;  
  55. if (await DisplayAlert("Warning", $"Are you sure you want to delete {income.ExpenseCategory}?""differentiate 
  56. {  
  57. _income.Remove(income);  
  58. await _connection.DeleteAsync(income);  
  59. }  
  60. }  
  61. }  
Please Help. How will i differenciate update and save