博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓初級教程(3):ContentProvider的運用原理
阅读量:5057 次
发布时间:2019-06-12

本文共 5189 字,大约阅读时间需要 17 分钟。

1 package com.example.android.provider; 2  3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.Map; 6 import android.app.Activity; 7 import android.content.ContentValues; 8 import android.database.Cursor; 9 import android.net.Uri;10 import android.os.Bundle;11 import android.widget.ListView;12 import android.widget.SimpleAdapter;13 14 //這是第一個先看的java檔,testprovider.java是繼承這個java檔的。15 16 public class ProviderTest extends Activity {17 //ProvideTest18     /** Called when the activity is first created. */19     @Override20     public void onCreate(Bundle savedInstanceState) {21         super.onCreate(savedInstanceState);22         //Content Provider和Uri。我們要學習的重中之重就在以下這兩行。23         //在這裡getIntent()是意圖從另外一個java檔中獲取數據。如何從另一個java檔中抓取數據,就是借助Uri.parse。24         //Uri uri_test是定義項,為了之後在Cusor cur之中抓取數據。25         getIntent().setData(Uri.parse("content://com.example.android.provider.testprovider"));26         Uri uri_test = getIntent().getData();27         28         //精妙之處就在於這個values和另一個java檔之中的values是處於何種關係?手29         ContentValues values = new ContentValues();30         values.put("name", "陳翹鈞");31         values.put("description", "68206021");32         getContentResolver().insert(uri_test, values);33         values.put("name", "馮幹傑");34         values.put("description", "60746017");35         getContentResolver().insert(uri_test, values);36         //Content Provider37         Cursor cur = managedQuery(uri_test, null, null, null, null);38         cur.moveToFirst();39         //ArrayList40         ArrayList
> data = new ArrayList
>();41 Map
item;42 //ArrayList data43 for (int i = 0; i < cur.getCount(); i++) {44 item = new HashMap
();45 item.put("column00", cur.getString(0));46 item.put("column01", cur.getString(1));47 item.put("column02", cur.getString(2));48 data.add(item);49 cur.moveToNext();50 }51 cur.close();52 //ArrayList data,mListView0153 ListView mListView01 = new ListView(this);54 SimpleAdapter adapter = new SimpleAdapter(this, data,55 R.layout.main, new String[] {"column00", "column01", "column02" }, new int[] {56 R.id.TextView01, R.id.TextView02, R.id.TextView03 });57 mListView01.setAdapter(adapter);58 setContentView(mListView01); 59 }60 }
1 package com.example.android.provider; 2  3 import android.content.ContentProvider; 4 import android.content.ContentValues; 5 import android.content.Context; 6 import android.database.Cursor; 7 import android.database.sqlite.SQLiteDatabase; 8 import android.database.sqlite.SQLiteOpenHelper; 9 import android.database.sqlite.SQLiteQueryBuilder;10 import android.net.Uri;11 import android.provider.BaseColumns;12 13 public class TestProvider extends ContentProvider {14     15     private static class DatabaseHelper extends SQLiteOpenHelper {16         17         DatabaseHelper(Context context) {18             super(context, "test.db", null, 1);19         }20         21         @Override22         public void onCreate(SQLiteDatabase db) {23             db.execSQL("CREATE TABLE test (" + BaseColumns._ID24                     + " INTEGER PRIMARY KEY," + "name TEXT,"25                     + "description TEXT" + ");");26         }27         28         @Override29         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {30             // TODO Auto-generated method stub31             db.execSQL("DROP TABLE IF EXISTS test");32             onCreate(db);33         }34     }35     //DatabaseHelper,databaseHelper36     DatabaseHelper databaseHelper;37     //Content Providers,onCreate()38     @Override39     public boolean onCreate() {40         databaseHelper = new DatabaseHelper(getContext());41         return true;42     }43     //Content Providers,insert()44     @Override45     public Uri insert(Uri uri, ContentValues values) {46         SQLiteDatabase db = databaseHelper.getWritableDatabase();47         db.insert("test", null, values);48         return null;49     }50     //Content Providers,query()51     @Override52     public Cursor query(Uri uri, String[] projection, String selection,53             String[] selectionArgs, String sortOrder) {54         SQLiteDatabase db = databaseHelper.getReadableDatabase();55         SQLiteQueryBuilder qb = new SQLiteQueryBuilder();56         qb.setTables("test");57         Cursor c = qb.query(db, projection, selection, selectionArgs, null,58                 null, null);59         return c;60     }61     //Content Providers,delete()62     @Override63     public int delete(Uri uri, String selection, String[] selectionArgs) {64         // TODO Auto-generated method stub65         return 0;66     }67     //Content Providers,getType()68     @Override69     public String getType(Uri uri) {70         // TODO Auto-generated method stub71         return null;72     }73     //Content Providers,update()74     @Override75     public int update(Uri uri, ContentValues values, String selection,76             String[] selectionArgs) {77         // TODO Auto-generated method stub78         return 0;79     }80 }

 

转载于:https://www.cnblogs.com/kanchihong/p/4522760.html

你可能感兴趣的文章
使用guava带来的方便
查看>>
关于Your job is to put yourself out of work
查看>>
C++中如何可以修改const函数内的成员变量的值?
查看>>
Cake
查看>>
2014-10月份步行记录
查看>>
mySQL程序与数据库连接帮助
查看>>
s3c2440的LCD字符显示
查看>>
Codeforces Round #313 (Div. 2)
查看>>
0120 父类与子类创建、重写及转型练习
查看>>
Android 读取MCC, MNC
查看>>
bzoj1625:[Usaco2007 Dec]宝石手镯(背包dp板子)
查看>>
微信QQ已经红了的域名怎么做跳转防封
查看>>
ES系列六、ES字段类型及ES内置analyzer分析
查看>>
流程控制之 while 循环
查看>>
Python 模块:random 随机数生成
查看>>
Quartus II 12.0 下载、安装和破解
查看>>
网络编程
查看>>
curl学习笔记(以php为例)
查看>>
WGS84 2 GCJ-02
查看>>
【TCP传输数据】
查看>>