本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/
现在,ContentProvider已经创建好了,可以去尝试使用一下。
1. 使用之前的工程,在布局文件main.xml中添加一些控件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ISBN" />
<EditText
android:id="@+id/txtISBN"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Title" />
<EditText
android:id="@+id/txtTitle"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
<Button
android:text="Add title"
android:id="@+id/btnAdd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickAddTitle" />
<Button
android:text="Retrieve titles"
android:id="@+id/btnRetrieve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="onClickRetrieveTitles" />
</LinearLayout>
2. 在ContentProvidersActivity.java中,添加测试代码。
public class ContentProvidersActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void onClickAddTitle(View view) {
/*
//---add a book---
ContentValues values = new ContentValues();
values.put(BooksProvider.TITLE, ((EditText)
findViewById(R.id.txtTitle)).getText().toString());
values.put(BooksProvider.ISBN, ((EditText)
findViewById(R.id.txtISBN)).getText().toString());
Uri uri = getContentResolver().insert(
BooksProvider.CONTENT_URI, values);
*/
ContentValues values = new ContentValues();
values.put("title", ((EditText)
findViewById(R.id.txtTitle)).getText().toString());
values.put("isbn", ((EditText)
findViewById(R.id.txtISBN)).getText().toString());
Uri uri = getContentResolver().insert(
Uri.parse(
"content://net.manoel.provider.Books/books"),
values);
Toast.makeText(getBaseContext(),uri.toString(),
Toast.LENGTH_LONG).show();
}
public void onClickRetrieveTitles(View view) {
//---retrieve the titles---
Uri allTitles = Uri.parse(
"content://net.manoel.provider.Books/books");
Cursor c;
if (android.os.Build.VERSION.SDK_INT <11) {
//---before Honeycomb---
c = managedQuery(allTitles, null, null, null,
"title desc");
} else {
//---Honeycomb and later---
CursorLoader cursorLoader = new CursorLoader(
this,
allTitles, null, null, null,
"title desc");
c = cursorLoader.loadInBackground();
}
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(
BooksProvider._ID)) + ", " +
c.getString(c.getColumnIndex(
BooksProvider.TITLE)) + ", " +
c.getString(c.getColumnIndex(
BooksProvider.ISBN)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
public void updateTitle() {
ContentValues editedValues = new ContentValues();
editedValues.put(BooksProvider.TITLE, "Android Tips and Tricks");
getContentResolver().update(
Uri.parse(
"content://net.manoel.provider.Books/books/2"),
editedValues,
null,
null);
}
public void deleteTitle() {
//---delete a title---
getContentResolver().delete(
Uri.parse("content://net.manoel.provider.Books/books/2"),
null, null);
//---delete all titles---
getContentResolver().delete(
Uri.parse("content://net.manoel.provider.Books/books"),
null, null);
}
}
分享到:
相关推荐
Android高级编程雪梨作业之自定义ContentProvider 将任务01生词本作业中生成的生词本数据库通过自定义ContentProvider的方式,共享给其他应用。 要求如下: (1) 使用自定义SQLiteOpenHelper来管理数据库; (2) 提交...
android 数据库 以及自定义ContentProvider demo
NULL 博文链接:https://sunzone.iteye.com/blog/1884167
这是学习ContentProvider的第二个练习。 里面自定义ContentProvider类来与SQLite交互。 大致类容就是:通过ContentProvider,创建自己的.db,操作自己的.db. 代码里有详细的解释
android 自定义 ContentProvider 以及 ContentResolver
ContentProvider自定义以及使用系统ContentProvider
自定义contentprovider 在其它应用中访问该应用暴露的数据。
NULL 博文链接:https://catkingwong.iteye.com/blog/1313740
自定义ContentProvider完成数据交互,文章:http://write.blog.csdn.net/postedit/43950781
在android中自定义一个ContentProvider大致需要四步:1,定义数据的MetaData;2,定义一个类继承ContentProvider;3,覆写自定义类中的getType(),onCreate(),insert(),delete(),update(),query()六个方法;4,在...
Android应用开发使用ContentProvider以及SQLite实现对数据库的相关操作,可以直接操作数据库,也可以使用ContentProvider来对数据库进行间接操作。
android自定义ContentProvider,可以实现程序之间共享数据
1、Android Room操作SQLite数据 2、ContentProvider使用,使用Room进行数据库操作
Android 自定义ContentProvider简单实例 Android允许我们定义自己的的ContentProvider对象来共享数据,练练手,简单来实现一下。 要使用ContentProvider来操作数据,必须要有保存数据的场所。可以使用文件或SQLite...
包含了常用的contentprovider操作,读取联系人,读取彩信,读取通话记录,自定义的contentprovider等。
主要介绍了Android开发教程之ContentProvider数据存储的相关资料,需要的朋友可以参考下
Android 中自定义ContentProvider与ContentObserver的使用简单实例 示例说明: 该示例中一共包含两个工程。其中一个工程完成了自定义ContentProvider,另外一个工程用于测试该自定义ContentProvider且在该工程中使用...
资源包涵两个Demo,其中一个是自定义的ContentProvider,另一个demo是访问第一个demo的ContentProvider并且去读写第一个demo的数据库。
此为ContentResolver与ContentProvider的使用Demo.