protected override async void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); InitializeLocationManager(); data = new WeatherDatabase(); await data.CreateDatabase(); button = FindViewById <Button>(Resource.Id.getWeatherButton); Button history = FindViewById <Button>(Resource.Id.buttonHistory); addressName = FindViewById <TextView>(Resource.Id.addressName); date = FindViewById <TextView>(Resource.Id.textDate); station = FindViewById <TextView>(Resource.Id.textStation); temperature = FindViewById <TextView>(Resource.Id.textTemp); pressure = FindViewById <TextView>(Resource.Id.textPressure); wind = FindViewById <TextView>(Resource.Id.textWind); if (Intent.GetStringExtra("MyData") != null) { WeatherObservation xd = JsonConvert.DeserializeObject <WeatherObservation>(Intent.GetStringExtra("MyData")); SetWeatherLabelsFromHistory(xd); } button.Click += AddressButton_OnClick; history.Click += async delegate { list = await data.GetDataFromDatabase(); list.Reverse(); StartActivity(typeof(Activity1)); }; }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); SetContentView(Resource.Layout.layout1); List <WeatherObservation> listToView = MainActivity.list; List <string> stationNameList = new List <string>(); listView = FindViewById <ListView>(Resource.Id.listView1); Button back = FindViewById <Button>(Resource.Id.buttonBack); Button delete = FindViewById <Button>(Resource.Id.buttonDelete); stationNameList.Clear(); foreach (var weather in listToView) { stationNameList.Add(weather.ID.ToString() + " " + weather.stationName); if (stationNameList.Count >= 5) { break; } } //ArrayAdapter<WeatherObservation> adapter = new ArrayAdapter<WeatherObservation>(this, Android.Resource.Layout.SimpleListItem1, listToView); ArrayAdapter <string> adapter = new ArrayAdapter <string>(this, Android.Resource.Layout.SimpleListItem1, stationNameList); listView.Adapter = adapter; listView.ItemClick += async(sender, args) => { string stringFromList = stationNameList[args.Position]; string onlyId = Regex.Match(stringFromList, @"\d+").Value; int id = int.Parse(onlyId); WeatherObservation fdb = await wdb.GetDataFromDatabaseByID(id); //MainActivity.SetWeatherLabelsFromHistory(fdb); Intent intent = new Intent(this, typeof(MainActivity)); intent.PutExtra("MyData", JsonConvert.SerializeObject(fdb)); Console.WriteLine("Retrieve data: " + stringFromList + " " + id); StartActivity(intent); }; back.Click += (sender, args) => { OnBackPressed(); }; delete.Click += (sender, args) => { wdb.RemoveDataFromDatabase(); }; }
public void SetWeatherLabelsFromHistory(WeatherObservation weather) { if (weather == null) { Console.Out.WriteLine("Weather is not obtained"); } else { //Console.Out.WriteLine("Station name : " + obj.weatherObservation.stationName); date.Text = "Date: " + weather.datetime; station.Text = "Station: " + weather.stationName + "; Country: " + weather.countryCode; temperature.Text = "Temperature: " + weather.temperature; pressure.Text = "Pressure: " + weather.hectoPascAltimeter + "; Humidity: " + weather.humidity; wind.Text = "Wind speed: " + weather.windSpeed + "; Direction: " + weather.windDirection; } }
public async Task <string> InsertUpdateData(WeatherObservation data) { try { var db = new SQLiteAsyncConnection(pathToDatabase); if ((await db.InsertAsync(data)) != 0) { await db.UpdateAsync(data); } Console.WriteLine("Data inserted or updated"); return("Single data file inserted or updated"); } catch (SQLite.SQLiteException ex) { Console.WriteLine("Error while inserting"); return(ex.Message); } }
public async Task <WeatherObservation> GetDataFromDatabaseByID(int ID) { try { WeatherObservation weatherToReturn = new WeatherObservation(); var db = new SQLiteAsyncConnection(pathToDatabase); var query = db.Table <WeatherObservation>().Where(v => v.ID == ID); await query.ToListAsync().ContinueWith(t => { foreach (var weather in t.Result) { weatherToReturn = weather; Console.WriteLine("Weather ID found: " + weather.ID); } }); return(weatherToReturn); } catch (SQLite.SQLiteException ex) { Console.WriteLine(ex.Message); return(null); } }