Reputation: 171
I have a mobile app and am testing the Android piece of it and have found an issue with moving data to a UI CollectionView from the xaml.cs file. I have a list of cars that show up on the main menu, but when I add a car, it is supposed to be automatically added to the list on this main memu which is not happening no matter how I attempt it. I was told in an earlier question I posted that I needed to define 2 observableCollections as follows;
ObservableCollection<AutoWithSwitch> MyList = new ObservableCollection<AutoWithSwitch>();
public ObservableCollection<AutoWithSwitch> AutoData
{
get { return MyList; }
set { MyList = value; }
}
I then populate MyList in a loop and once populated, I move it to AutoData which is the ItemSource for my CollectionView. This works all over my code but falls down in this instance. Here is my code in the EnterCarViewModel where I have the code to add a car to my database.
int results = await repository.**AddAutoData(data);**
if (results == 1)
{
IsVisibleResults = true;
EntResults = "Entered Successfully";
if (Convert.ToBoolean(Preferences.Default.Get("firstAutoEntered", false)))
{
Preferences.Set("autoId", 1);
Preferences.Set("firstAutoEntered", false);
}
Preferences.Set("CarAdded", true);
**mmvm.GetMyCars(await repository.GetAuto());**
await Shell.Current.GoToAsync("///MainPage");
}
As you can see in the top line, I am adding the car to my DB table. Then in the second to the last line, I am calling GetMyCar method in my MainMenuViewModel where I attempt to get a list of the cars and display them on my main menu.
public async void GetMyCars(List<AutoTableDefination> atd)
{
try
{
bool processed = false;
MileageItemRepository repo = new MileageItemRepository();
MyList = new ObservableCollection<AutoWithSwitch>();
AutoData = new ObservableCollection<AutoWithSwitch>();
var autoId = Convert.ToInt32(Preferences.Get("autoId", 0));
if (atd.Count > 0)
{
foreach (var item in atd)
{
if (item.CarDesc.ToUpper() == Preferences.Default.Get("UpperCar", string.Empty))
{
if (item.Id == autoId)
{
item.CarDesc = item.CarDesc.ToUpper();
Preferences.Default.Set("UpperCar", item.CarDesc.ToUpper());
Preferences.Default.Set("autoId", item.Id);
}
}
if (item.IsDefault)
item.CarDesc = "**" + item.CarDesc;
if (item.IsDefault && !processed)
{
Preferences.Default.Set("autoId", item.Id);
}
Preferences.Default.Set("enter1stCar", false);
if (!processed)
{
processed = false;
}
MyList.Add(new AutoWithSwitch { Id = item.Id, Name = item.CarDesc });
}
var sortedList =
MyList.OrderBy(x => x.Name);
MyList = new ObservableCollection<AutoWithSwitch>(sortedList);
AutoData = MyList;
MainPage mainPage = new MainPage(AutoData, cv);
//cv.ItemsSource = null;
//cv.ItemsSource = AutoData;
processed = true;
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
if (Convert.ToBoolean(Preferences.Get("CarAdded", true)))
Device.BeginInvokeOnMainThread(async () => { await
App.Current.MainPage.DisplayAlert("Success", "Car successfully added", "Ok"); });
}
As you can see, I have MyList and AutoData initialized at the top of the method. I then loop through and load my data into MyList, sort that list and then move it into AutoData which again is my ItemSource on this menu page. I have tried both ways, move it directly to itemSource in the next 2 lines or move it in the MainPage constructor, neither work. Again, this works all over in my code so not sure why it is not here.
If anyone has an idea of what is wrong here, you help would be much appreciated. Thanks!
Upvotes: 0
Views: 65