【发布时间】:2019-02-01 16:02:00
【问题描述】:
我有一个从用户设备获取经度和纬度并将其保存到 SQLite 数据库的类。代码如下:
[Activity(Label = "GetLocation")]
public class GetLocation : Activity, ILocationListener
{
Button btncreate;
EditText txtlong;
EditText txtlat;
static readonly string TAG = "X:" + typeof(GetLocation).Name;
Location _currentLocation;
LocationManager _locationManager;
string _locationProvider;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.GetLocation);
txtlong = FindViewById<EditText>(Resource.Id.txtlong);
txtlat = FindViewById<EditText>(Resource.Id.txtlat);
btncreate = FindViewById<Button>(Resource.Id.btnvalidate);
btncreate.Click += Btncreate_Click;
InitializeLocationManager();
}
void InitializeLocationManager()
{
_locationManager = (LocationManager) GetSystemService(LocationService);
Criteria criteriaForLocationService = new Criteria
{
Accuracy = Accuracy.Fine
};
IList<string> acceptableLocationProviders = _locationManager.GetProviders(criteriaForLocationService, true);
if (acceptableLocationProviders.Any())
{
_locationProvider = acceptableLocationProviders.First();
}
else
{
_locationProvider = string.Empty;
}
Log.Debug(TAG, "Using " + _locationProvider + ".");
}
public void OnProviderDisabled(string provider) { }
public void OnProviderEnabled(string provider) { }
public void OnStatusChanged(string provider, Availability status, Bundle extras) { }
protected override void OnResume()
{
base.OnResume();
_locationManager.RequestLocationUpdates(_locationProvider, 0, 0, this);
}
protected override void OnPause()
{
base.OnPause();
_locationManager.RemoveUpdates(this);
}
public void OnLocationChanged(Location location)
{
_currentLocation = location;
if (_currentLocation == null)
{
txtlong.Text = "Unable to determine your location. Try again in a short while.";
txtlat.Text = "Unable to determine your location. Try again in a short while.";
}
else
{
txtlong.Text = _currentLocation.Longitude.ToString();
txtlat.Text = _currentLocation.Latitude.ToString();
}
}
private void Btncreate_Click(object sender, EventArgs e)
{
try
{
string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Location.db3");
var db = new SQLiteConnection(dpPath);
db.CreateTable<DataTable>();
DataTable tbl = new DataTable();
tbl.Longitude = txtlong.Text;
tbl.Latitude = txtlat.Text;
db.Insert(tbl);
NextPage();
Toast.MakeText(this, "Data Store Successfully...,", ToastLength.Short).Show();
}
catch (Exception ex)
{
Toast.MakeText(this, ex.ToString(), ToastLength.Short).Show();
}
}
void NextPage()
{
//code to get next page
}
}
DataTable类如下:
class DataTable
{
[PrimaryKey, AutoIncrement, Column("_Id")]
public int Id {get; set;}
public string Longitude { get; set; }
public string Latitude { get; set; }
}
一旦它保存了数据,我希望另一个类(活动)能够在单独的 .axml 页面上显示它。我的问题是我是一名正在接受培训(大学)的程序员,不知道该怎么做。非常感谢您的帮助。
【问题讨论】:
-
请出示您的
DataTable。编辑您的问题并提出。我很困惑?
标签: c# sqlite xamarin.android