现在,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. */@Overridepublic 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);}}


更多相关文章

  1. Android(安卓)给Button加个监听
  2. Android原生分享图片和视频
  3. Google Map API V2
  4. Android(安卓)GridView等控件的属性集合
  5. Android(安卓)NDK开发之旅38--FFmpeg视频添加水印
  6. 好用的Android的UI第三方开源框架
  7. Android(安卓)ApiDemos示例解析(179):Views->Lists->12. Transcr
  8. Android(安卓)获取控件高度宽度三种方法,防止0的出现
  9. 安卓开发----TextView控件属性列表

随机推荐

  1. Android(安卓)View回顾之坐标系
  2. 微软和Wistron再度联手,Android和Chrome O
  3. (一)Android应用程序及组件简介
  4. ubuntu linux通过adb命令行"复制粘贴"内
  5. 【Android实战开发】3G技术和Android发展
  6. Android中Handler异步线程
  7. AndroidManifest.xml 详解 (四) 之uses-p
  8. Android设备到底侵犯了微软的什么专利
  9. (三)Android(安卓)Context说明
  10. Android(安卓)的广播机制和两种注册方式