带有dot42的异步SQLite示例? [英] Asynchronous SQLite sample with dot42?

查看:79
本文介绍了带有dot42的异步SQLite示例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我一直在测试dot42,但我发现没有任何SQLite示例.我不确定是否应该实现ContentProvider(如一些android文章所建议的那样),或者我是否可以使用dot42的异步/等待实现来异步执行查询并在ListView上显示结果.

I've been testing dot42 and so far great, but I see that there aren't any SQLite samples. Im not sure if I should implement a ContentProvider (as some android articles suggests) or if I could use the async / wait implementation of dot42 in order to perform a query asynchronously and show the result on a ListView.

有什么建议吗?

预先感谢

Roygar

推荐答案

此处是一个代码示例,该示例使用dot42的async/await实现从SQLite数据库异步检索联系人.我省略了SQLite代码. ContactsDatabaseSQLiteOpenHelper继承并实现通常的方法.

Here is a code sample that retrieves contacts from a SQLite database asynchronously using the async/await implementation of dot42. I omitted the SQLite code. ContactsDatabase inherits from SQLiteOpenHelper and implements the usual methods.

using System;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;

using Android.App;
using Android.Os;
using Android.Widget;

using Dot42;
using Dot42.Manifest;

[assembly: Application("SQLiteAsyncSample")]

namespace SQLiteAsyncSample
{
   [Activity]
   public class MainActivity : Activity
   {
      private ArrayAdapter<string> adapter;
      ContactsDatabase database;
      int i = 0;

      protected override void OnCreate(Bundle savedInstance)
      {
         base.OnCreate(savedInstance);
         SetContentView(R.Layouts.MainLayout);

         ListView list = FindViewById<ListView>(R.Ids.list);
         adapter = new ArrayAdapter<string>(this, Android.R.Layout.Simple_list_item_1);
         list.SetAdapter(adapter);

         database = new ContactsDatabase(this);

         database.AddContact(new Contact("Frank", "012"));
         database.AddContact(new Contact("Marco", "345"));
         database.AddContact(new Contact("Hans", "678"));
         database.AddContact(new Contact("Sergey", "901"));

         Button addAllButton = FindViewById<Button>(R.Ids.showall);
         addAllButton.Click += showAllButton_Click;

         // Set the static synchronization context to the current/latest 'this'.
         // This allows the code after the wait to resume on the 'current' this
         // even if the Activity was recycled, e.g. due to a device rotation.
         SynchronizationContext.SetSynchronizationContext(this);
      }

      private async void showAllButton_Click(object sender, EventArgs e)
      {
         List<Contact> contacts = null;
         await Task.Factory.StartNew( () => {
            // lengthy job
            contacts = database.GetAllContacts();
         }).ConfigureAwait(this);

         // make sure to access the adapter from the UI thread
         // so not in the anonymous delegate above
         foreach (Contact contact in contacts) {
            adapter.Add(contact.Name);
         }
      }
   }
}

这篇关于带有dot42的异步SQLite示例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆