【问题标题】:Retrieve data from SQLite using xamarin Android & C#?使用 xamarin Android 和 C# 从 SQLite 检索数据?
【发布时间】: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


【解决方案1】:

你需要开始一个新的活动使用

StartActivity(typeof(Activity2));

并在您的Activity2OnCreate 中为您设置新的.axml 布局:

protected override void OnCreate(Bundle savedInstanceState)
{
    base.OnCreate(savedInstanceState);
    SetContentView(Resource.Layout.MyView);
}

然后使用 Query 检索数据:

string dpPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "Location.db3");
var db = new SQLiteConnection(dpPath);
var dataTables = db.Query<DataTable>("select * from DataTable");

我建议您阅读SQLite: GettingStarted,如果您想改进这一点,请将有关 SQLite 的所有内容移至另一个类以重用打开SQLiteConnection 的代码。此外,如果您可以使用异步调用来防止在将数据持久化到数据库中时阻塞 UI。

还可以查看Xamarin Android docs,这真的很有帮助

【讨论】:

  • 您好,我需要在 .axml 文件中添加一些特定的内容吗?因为当活动加载时我得到一个空白页面
  • 是的,当然。您需要在您的 axml 中进行活动的布局。如果很简单,你可以使用设计师的帮助docs.microsoft.com/en-us/xamarin/android/user-interface/…
  • 它需要&lt;ListView&gt; 才能显示。
猜你喜欢
  • 1970-01-01
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-07
  • 2023-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多