getContentResolver().insert(NotePad.Notes.CONTENT_URI, values);
displayNote();
}
private void displayNote() {
// TODO Auto-generated method stub
String columns[]=new String[]{
NotePad.Notes._ID ,
NotePad.Notes.TITLE,
NotePad.Notes.NOTE ,
NotePad.Notes.CREATEDDATE,
NotePad.Notes.MODIFIEDDATE };
Uri myUri=NotePad.Notes.CONTENT_URI;
Cursor cur=managedQuery(myUri,columns,null,null,null);
if (cur.moveToFirst()){
String id=null;
String title=null;
do{
id=cur.getString(cur.getColumnIndex(NotePad.Notes._ID ));
title=cur.getString(cur.getColumnIndex(NotePad.Notes.TITLE));
Toast.makeText (this, "TITLE:"+id+"Note:"+title,
Toast.LENGTH_LONG ).show();
}while (cur.moveToNext());
}
}
}
最后我们需要注意不要忘记在Androidmanifest.xml 中声明我们使用的ContentProvider,具体
代码如代码清单6-9所示:
代码清单 6-9 第六章\CH6_5\AndroidManifest.xml
android:authorities= "vider.NotePad"/>
android:label="@string/app_name">
android:name="ent.category.LAUNCHER" />
android:mimeType="vnd.android.cursor.dir/vnd.google.note"/>
android:mimeType="vnd.android.cursor.item/vnd.google.note"/>
程序运行截图如图6-12 所示:
图6-12 Toast 显示数据库中数据
本章小结
在本章我们主要介绍了Android 中5 中数据存储的方式,分别为:Shared Preferences、
Files 、Networks 、SQLite 以及Content Provider 。我们注重介绍了使用比较多的SQLite 数据
库的使用。由于Android 中数据基本是私有的,因此最后我们通过介绍ContentProvider 来实
现各个不同应用程序之间数据的传输和共享。