mikebai.com

  • Home
  • dev
  • DotNET
  • M365
  • 搞笑
  • 杂七杂八
  • FocusDict
dev
dev

SQLiteOpenHelper和ContentProvider区别

Android中操作数据库主要有两种方法:使用SQLiteOpenHelper 和使用ContentProvider。  (一)使用SQLiteOpenHelper:一个抽象类,用于提供管理数据库版本并维护创建数据库的接口。其子类必须实现onCreate(SQLiteDatabase)和onUpdate(SQLiteDatabase, int, int)方法,也可以实现可选方法onOpen(SQLiteDatabase)。另外,也必须重写父类的构造函数。  如果数据库存在则这个类将负责打开数据库,如果不存在则创建一个新的数据库。当Android检测到你在引用一个旧数据库(根据版本号判断)时,它将调用onUpdate()方法。  1.添加数据:  》实例化一个继承了SQLiteOpenHelper抽象类的类,(例如为events);  》获取SQLiteDatabase对象:SQLiteDatabase db = events.getWritableDatabase();  》定义ContentValues的一个对象用于存储数据:ContentValues values = new ContentValues();  》调用ContentValues的put方法装载数据;  》将数据插入数据库中:db.insertOrThrow(TABLE_NAME, null, values);  其中SQLiteOpenHelper类中的getReadableDatabase()和getWritableDatabase()及close()方法都是同步方法。  顾名思义,如果insertOrThrow()执行失败,它可以抛出一个SQLException类型的异常。无需使用一个throws关键字来声明该异常,因为它是一个RuntimeException,而不是一个检查异常。但是,如果你喜欢,仍然可以在一个try/catch块中处理它,就像处理任何其他异常一样。如果未处理它并且存在一个错误,程序将终止,并将一条回溯信息转储到Android日志中。  2.查询数据:  因为无需因查询而修改数据库,所以可以只调用getReadableDatabase()获得一个只读句柄。  》SQLiteDatabase db = events.getReadableDatabase();  》Cursor cursor = db.query(TABLE_NAME, FROM, null,null,null,null,ORDER_BY);  》startManagingCursor(cursor);  其中startManagerCursor():告诉活动应根据该活动的生命周期来管理光标的生命周期。例如:当活动被暂停时,它将自动停止用光标,然后在活动重启时重新查询该光标。当活动终止时,所有托管的光标都将关闭。  3:数据绑定  用于数据表中存在大量数据的情况下,可以提高程序的运行速度。  如果要将大量的数据显示在View中,并将这些数据追加在一个字符串中,则可能耗尽所有内存。  ListView与Adapter的结合使用。  如果数据源是在XML中定义的一个数组,则使用ArrayAdapter;如果数据源是一个来自于数据库查询的Cursor对象,则使用SimpleCursorAdapter。  Java代码   public class DatabaseHelper extends SQLiteOpenHelper {       //构造,调用父类构造,数据库名字,版本号(传入更大的版本号可以让数据库升级,onUpgrade被调用)  

2017-06-19 0comments 110hotness 0likes mikebai Read all
dev

Handling Offline Capability and Data Sync In An Android* App – Part 1

https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-1 Abstract Mobile apps with a backend need to provide offline capability as the devices may not have continuous network access. We also need an efficient way for our app to automatically sync with a backend server. In this article we will take a look at how Android* sync adapter framework can be leveraged for a restaurant sample app to enable seamless data sync. We will discuss how to provide offline capability using content providers and local SQLite database. This will be a 2-part series; part 1 will cover the usage of content provider APIs with a local SQLite database. Contents Abstract. Overview... A Retail Business Restaurant Sample App – Little Chef. Using a Content Provider with local SQLite database. Implementing the RestaurantContentProvider Accessing RestaurantContentProvider using ContentResolver Adding RestaurantContentProvider to app Manifest. About the Author Overview Android* apps that rely on a backend server for data or content need to provide offline capability for a seamless userexperience. This requires us to maintain a local copy of the data model. We also need an efficient way to keep the local data model insync with the one on the backend server. We can achieve both of these features using the standard Android APIs. Content provider APIs can be used to abstract away the data model, and using SQLite APIs we can maintain a device resident SQLite database for a local copy of the data. For efficient data sync with the server, Android provides sync adapter framework APIs to automatically handle network connection drops, background syncing, and scheduling. Additionally, we can hook up…

2017-06-18 0comments 121hotness 0likes mikebai Read all
dev

Handling Offline Capability and Data Sync in an Android* App – Part 2

Download PDF https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-2 Abstract Mobile apps that rely on backend servers for their data needs should provide seamless offline capability. To provide this capability, apps must implement a data sync mechanism that takes connection availability, authentication, and battery usage, among other things, in to account. In Part 1, we discussed how to leverage the Android sync adapter framework to implement these features in a sample restaurant app, mainly using content provider. In this part we will explain the remaining pieces, the sync adapter and authenticator. We will also look at how to use Google cloud messaging (GCM) notifications to trigger the data sync with a backend server. Contents Abstract Overview Data Sync Strategy for Restaurant Sample App – Little Chef Sync Adapter Implementation Authenticator Implementation Configuring and Triggering the Sync About the Author Overview If you haven’t already read Part 1, please refer to the following link: https://software.intel.com/en-us/articles/handling-offline-capability-and-data-sync-in-an-android-app-part-1 Part 1 covers the integration of content provider with our sample app, which uses local SQLite database. Though the content provider is optional for sync adapter, it abstracts the data model from other parts of the app and provides a well-defined API for integrating with other components of Android framework (for example, loaders). To fully integrate Android sync adapter framework into our sample app, we need to implement the following pieces: sync adapter, a sync service that links the sync adapter with Android sync framework, authenticator, and an authenticator service to bridge the sync adapter framework and authenticator. For the authenticator we will use a dummy account for demo purposes. Data Sync…

2017-06-18 0comments 99hotness 0likes mikebai Read all
dev

调用google api

https://console.developers.google.com/apis 首先创建凭据  包名,keystore文件的sha1码 API管理器中启用相应的api

2017-06-13 0comments 113hotness 0likes mikebai Read all
dev

创建keystore文件

android studio 菜单 -> buid -> Generate Signed Apk 创建jks后缀的签名件  然后在build.grade 中引用 android { signingConfigs { config01 { keyAlias 'MXXXXE' keyPassword 'XXXXX' storeFile file("E:/XXXXX/focusdict.jks") storePassword 'XXXXX' }     } I have not fully fixed this issue, but I think it is related to the following entries in 'Telegram\TMessagesProj\build.gradle': signingConfigs {

2017-06-13 0comments 105hotness 0likes mikebai Read all
dev

コピペしてすぐ使えるアラートダイアログ集

画面回転時のメモリーリークを防ぐため、下記のような DialogFragment を作成することを推奨します。 public class SampleDialogFragment extends DialogFragment { @Override public Dialog onCreateDialog(Bundle savedInstanceState) { return new AlertDialog.Builder(getActivity()) .setTitle("タイトル") .setMessage("メッセージ") .create(); } @Override public void onPause() { super.onPause(); // onPause でダイアログを閉じる場合 dismiss(); } } はじめに あのタイプのダイアログってどう出すんだっけ、とよく忘れてしまうので、コピペしてすぐ使えるようにメモ。サンプルは Fragment 内で実行するケースのものであり、Negative ボタンはキャンセルボタンとして扱っています。 公式サイトの参考ページはこちら。 http://developer.android.com/guide/topics/ui/dialogs.html http://developer.android.com/reference/android/app/AlertDialog.Builder.html

2017-06-11 0comments 127hotness 0likes mikebai Read all
dev

AndroidStudio提交代码到svn忽略文件的设置

准备工作: svn仓库地址, 需要上传的Android工程 点击shareProject(Subversion)按钮 shareProject(Subversion) 配置仓库地址并关联 share 添加忽略配置, AndroidStudio下忽略文件必须在提交之前设置好, 一旦提交后就不能忽略了, 强行忽略或者试图删除并重新上传会出现奇怪的svn问题, 忽略文件的设置有两种方式: 在setting的version control中设置ignore; 右键想要忽略的文件或者文件夹进行忽略

2017-06-05 0comments 106hotness 0likes mikebai Read all
dev

Android studio NDK成长记录(一)CMake

Android studio NDK开发支持CMake和ndk-build两种编译方式: 第一种:CMake编译方式的开发步骤 下载安装studio     下载地址:“http://www.android-studio.org/” 新建项目 下载安装ndk插件:File右键->setting->Android SDK->SDK Tool勾选CMake,LLDB,NDK,将这三个插件全都安装上。 查看是否关联上NDK,也可以手动添加(local.properties末尾加上如下代码) //这是自己的ndk地址,注意转义,下面的地址本来是"E:\Android\SDK\ndk-bundle" ndk.dir=E\:\\Android\\sdk\\ndk-bundle

2017-06-01 0comments 113hotness 0likes mikebai Read all
dev

android studio中删除所有未使用的资源

You can easily search for unused resources from Android Studio. Just press Ctrl+Alt+Shift+iand type "Unused resources" (without quotes). That will execute lint. Super easy way how run lint commands (and other stuff from IDE). OR In Android Studio -> Menu -> Refactor -> Remove Unused Resources... Select the resources you want to remove. You can exclude resources you want to keep by right clicking on the resource item. Use Do Refactor to remove all Resources at once.

2017-01-22 0comments 116hotness 0likes mikebai Read all
dev

xampp-control 端口更改后管理页面url加入端口号

xampp安装路径下 E:\xampp\xampp-control.ini 更改以下设置为新端口号 [ServicePorts] Apache=8080

2017-01-22 0comments 116hotness 0likes mikebai Read all
1…678910…25

Recent Posts

  • c# winform适配高dpi
  • com.microsoft.sqlserver.jdbc.SQLServerException “trustServerCertificate”属性设置为“false”,但驱动程序无法使用安全套接字层 (SSL) 加密与 SQL Server建立安全连接
  • java -cp 用法介绍
  • HTML 容器元素
  • MVC的cshtml的介绍

Recent Comments

No comments to show.

COPYRIGHT © 2025 mikebai.com. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang