Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 初級開發 >> android contentProvider用法

android contentProvider用法

編輯:初級開發

存儲數據的方式很多,在android平時有sharedpreference file sqlite等.這裡主要分析下數據庫存儲.為了共享數據庫.可以讓數據庫披上contentprovider 外衣.具體看例子 
[size=12]package com.misoo.SQ02; 
import Java.util.ArrayList; 
import Java.util.HashMap; 
import Java.util.Map; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.Net.Uri; 
import android.os.Bundle; 
import android.view.VIEw; 
import android.widget.ListVIEw; 
import android.widget.SimpleAdapter; 
public class ac01 extends ListActivity { 
public static int g_variable; 
public static final String AUTHORITY = "com.misoo.provider.SQ02"; 
public static final Uri CONTENT_URI = 
Uri.parse("content://" + AUTHORITY + "/StudentTable"); 
private static final String[] PROJECTION = new String[] { 
"stud_no", "stud_name" }; 
@Override protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
Intent intent = getIntent(); 
if (intent.getData() == null) intent.setData(CONTENT_URI); 
Cursor cur = getContentResolver().query(getIntent().getData(), 
PROJECTION, null, null, null); 
ArrayList<Map<String, Object>> coll 
= new ArrayList<Map<String, Object>>(); 
Map<String, Object> item; 
cur.moveToFirst(); 
while(!cur.isAfterLast()) { 
item = new HashMap<String, Object>(); 
item.put("c1", cur.getString(0) + ", " + cur.getString(1)); 
coll.add(item); 
cur.moveToNext(); } 
this.setListAdapter(new SimpleAdapter(this, coll, 
android.R.layout.simple_list_item_1, new String[] { "c1" }, 
new int[] {android.R.id.text1})); } 
@Override 
protected void onListItemClick(ListView l, VIEw v, int position, long id){ 
finish(); } 
package com.misoo.SQ02; 
import android.content.ContentProvider; 
import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.Net.Uri; 
import android.util.Log; 
public class DataProvider extends ContentProvider { 
private static final String DATABASE_NAME = "StudDB_2.db"; 
private static final int DATABASE_VERSION = 2; 
private static final String TABLE_NAME = "StudentTable"; 
private static class DatabaseHelper extends SQLiteOpenHelper { 
DatabaseHelper(Context context) { 
super(context, DATABASE_NAME, null, DATABASE_VERSION); @Override public void onCreate(SQLiteDatabase db) { 
db.execSQL("Create TABLE " + TABLE_NAME + " (" 
+ "stud_no" + " TEXT," + "stud_name" + " TEXT" + ");"); 
String sql_1 = "insert into "+ TABLE_NAME + 
" (stud_no, stud_name) values('S108', 'Lily Chen');"; 
String sql_2 = "insert into " + TABLE_NAME + 
" (stud_no, stud_name) values('S201', 'Tom Kao');"; 
String sql_3 = "insert into " + TABLE_NAME + 
" (stud_no, stud_name) values('S333', 'Peter Rabbit');"; 
try { 
db.execSQL(sql_1); db.execSQL(sql_2); db.execSQL(sql_3); 
} catch (SQLException e) { Log.e("ERROR", e.toString()); } @Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} private DatabaseHelper mOpenHelper; 
@Override public boolean onCreate() { 
mOpenHelper = new DatabaseHelper(getContext()); 
return true; @Override public Cursor query(Uri uri, String[] projection, String selection, 
String[] selectionArgs, String sortOrder) { 
SQLiteDatabase db = mOpenHelper.getReadableDatabase(); 
Cursor c = db.query(TABLE_NAME, projection, null, null, null, null, null); 
return c; @Override public String getType(Uri uri) 
{ return null; } 
@Override public Uri insert(Uri uri, ContentValues initialValues) 
{ return uri; } 
@Override public int delete(Uri uri, String where, String[] whereArgs) 
{ return 0; } 
@Override public int update(Uri uri, ContentValues values, 
String where, String[] whereArgs) 
{ return 0; } 
}[/size] 從例子可以看出。不管怎麼表達,最後要得到SQLiteDatabase引用.然後用這個引用去操作數據庫.在這裡通過contentprovide 包了SQLlitOpenhelper ,通過這個來創建數據庫.創建表.然後通過getContentResolver 來操作數據庫.這個代碼應該不難,關鍵我想,系統如何知道我要操作哪個數據庫裡的哪個表呢? 這需要用戶指定 
public static final String AUTHORITY = "com.misoo.provider.SQ02"; 
public static final Uri CONTENT_URI = 
Uri.parse("content://" + AUTHORITY + "/StudentTable"); 這兩句就是指定了哪個數據庫,指定了哪個表. 這說明在manifest.XML 裡一定要加上這麼幾句 <provider android:name="DataProvider" 
android:authoritIEs="com.misoo.provider.SQ02"> 
</provider> 有了這個東西就知道是哪個數據了.就可以開始操作了.
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved