I don't know why I am getting this error. I am using MVVM and a listview. What I am trying to use is that I am fetching some info from Backend with the help of MVVM. I want that my listview should be updated when I am adding an item. Everything is working fine except the listview, neither it is updated nor unable to remove an item.
Here is my code
List<string> items = new List<string>();
ObservableCollection<UserCartDetails> cart_response = new ObservableCollection<UserCartDetails>();
ShoppingServices shoppingServices = new ShoppingServices();
//UserCartDetails Item = new UserCartDetails();
public static Context _context = Android.App.Application.Context;
#region "Refreshing Data"
private bool isLoading;
private bool isRefreshing;
public bool IsLoading
{
get
{
return this.isLoading;
}
set
{
this.isLoading = value;
RaisePropertyChanged("IsLoading");
}
}
public bool IsRefreshing
{
get
{
return this.isRefreshing;
}
set
{
this.isRefreshing = value;
RaisePropertyChanged("IsRefreshing");
}
}
public event PropertyChangedEventHandler PropertyChanged;
public void RaisePropertyChanged(string propName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
}
public Command RefreshCommand
{
get
{
return new Command(async () =>
{
await BindData();
});
}
}
#endregion
private async Task BindData()
{
//cart_response.Clear();
var response1 = shoppingServices.GetCartDetail(BaseClass.UserId, BaseClass.Kitchen_Id, BaseClass.Token);
if (response1.success == "True")
{
Device.BeginInvokeOnMainThread(() =>
{
cart_response = response1.parameters;
ResponseList.ItemsSource = cart_response;
});
// cart_response = response1.parameters;
}
IsLoading = false;
IsRefreshing = false;
}
private async void Button_Click(object sender, EventArgs e)
{
IsLoading = true;
data.IsVisible = true;
string name = entry_name.Text;
string quantity = WeightList.SelectedItem.ToString().Split(' ')[0];
var response = shoppingServices.CartDetail(BaseClass.UserId, BaseClass.Kitchen_Id, name, quantity, BaseClass.Token);
if (response.success == "True")
{
await Task.Run(async () => { await BindData(); });
//await BindData();
}
}
void OnTapGestureRecognizerTapped2(object sender, EventArgs e)
{
//var item = (Xamarin.Forms.Image)sender;
int cart_id = Convert.ToInt32(((Xamarin.Forms.TappedEventArgs)e).Parameter);
var response = shoppingServices.RemoveCartDetail(BaseClass.UserId, cart_id.ToString(), BaseClass.Token);
if (response.success == "True")
{
Toast.MakeText(_context, response.message, ToastLength.Long).Show();
cart_response.Clear();
ResponseList.ItemsSource = cart_response;
if(ResponseList.ItemsSource==null)
{
ResponseList.IsVisible = false;
}
else
{
var response1 = shoppingServices.GetCartDetail(BaseClass.UserId, BaseClass.Kitchen_Id, BaseClass.Token);
if (response1.success == "True")
{
cart_response = response1.parameters;
ResponseList.ItemsSource = cart_response;
}
}
}
else
{
Toast.MakeText(_context, response.message, ToastLength.Long).Show();
}
}