一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Android - Android內容提供者ContentProvider用法實例分析

Android內容提供者ContentProvider用法實例分析

2021-06-26 22:30Ruthless Android

這篇文章主要介紹了Android內容提供者ContentProvider用法,結合實例形式較為詳細的分析了內容提供者ContentProvider獲取及解析數據的相關技巧,需要的朋友可以參考下

本文實例講述了android內容提供者contentprovider用法。分享給大家供大家參考,具體如下:

personcontentprovider內容提供者類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.ljq.db;
import android.content.contentprovider;
import android.content.contenturis;
import android.content.contentvalues;
import android.content.urimatcher;
import android.database.cursor;
import android.database.sqlite.sqlitedatabase;
import android.net.uri;
import android.text.textutils;
/**
 * 內容提供者
 *
 * 作用:統一數據訪問方式,方便外部調用
 *
 * @author jiqinlin
 *
 */
public class personcontentprovider extends contentprovider {
  // 數據集的mime類型字符串則應該以vnd.android.cursor.dir/開頭
  public static final string persons_type = "vnd.android.cursor.dir/person";
  // 單一數據的mime類型字符串應該以vnd.android.cursor.item/開頭
  public static final string persons_item_type = "vnd.android.cursor.item/person";
  public static final string authority = "com.ljq.provider.personprovider";// 主機名
  /* 自定義匹配碼 */
  public static final int persons = 1;
  /* 自定義匹配碼 */
  public static final int person = 2;
  public static final uri persons_uri = uri.parse("content://" + authority + "/person");
  private dbopenhelper dbopenhelper = null;
  // urimatcher類用來匹配uri,使用match()方法匹配路徑時返回匹配碼
  private static final urimatcher urimatcher;
  static {
    // 常量urimatcher.no_match表示不匹配任何路徑的返回碼
    urimatcher = new urimatcher(urimatcher.no_match);
    // 如果match()方法匹配content://com.ljq.provider.personprovider/person路徑,返回匹配碼為persons
    urimatcher.adduri(authority, "person", persons);
    // 如果match()方法匹配content://com.ljq.provider.personprovider/person/230路徑,返回匹配碼為person
    urimatcher.adduri(authority, "person/#", person);
  }
  @override
  public boolean oncreate() {
    dbopenhelper = new dbopenhelper(this.getcontext());
    return true;
  }
  @override
  public uri insert(uri uri, contentvalues values){
    sqlitedatabase db = dbopenhelper.getwritabledatabase();
    long id = 0;
    switch (urimatcher.match(uri)) {
    case persons:
      id = db.insert("person", "name", values);// 返回的是記錄的行號,主鍵為int,實際上就是主鍵值
      return contenturis.withappendedid(uri, id);
    case person:
      id = db.insert("person", "name", values);
      string path = uri.tostring();
      return uri.parse(path.substring(0, path.lastindexof("/"))+id); // 替換掉id
    default:
      throw new illegalargumentexception("unknown uri " + uri);
    }
  }
  @override
  public int delete(uri uri, string selection, string[] selectionargs) {
    sqlitedatabase db = dbopenhelper.getwritabledatabase();
    int count = 0;
    switch (urimatcher.match(uri)) {
    case persons:
      count = db.delete("person", selection, selectionargs);
      break;
    case person:
      // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10
      // 進行解析,返回值為10
      long personid = contenturis.parseid(uri);
      string where = "id=" + personid;// 刪除指定id的記錄
      where += !textutils.isempty(selection) ? " and (" + selection + ")" : "";// 把其它條件附加上
      count = db.delete("person", where, selectionargs);
      break;
    default:
      throw new illegalargumentexception("unknown uri " + uri);
    }
    db.close();
    return count;
  }
  @override
  public int update(uri uri, contentvalues values, string selection,
      string[] selectionargs) {
    sqlitedatabase db = dbopenhelper.getwritabledatabase();
    int count = 0;
    switch (urimatcher.match(uri)) {
    case persons:
      count = db.update("person", values, selection, selectionargs);
      break;
    case person:
      // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10
      // 進行解析,返回值為10
      long personid = contenturis.parseid(uri);
      string where = "id=" + personid;// 獲取指定id的記錄
      where += !textutils.isempty(selection) ? " and (" + selection + ")" : "";// 把其它條件附加上
      count = db.update("person", values, where, selectionargs);
      break;
    default:
      throw new illegalargumentexception("unknown uri " + uri);
    }
    db.close();
    return count;
  }
  @override
  public string gettype(uri uri) {
    switch (urimatcher.match(uri)) {
    case persons:
      return persons_type;
    case person:
      return persons_item_type;
    default:
      throw new illegalargumentexception("unknown uri " + uri);
    }
  }
  @override
  public cursor query(uri uri, string[] projection, string selection,
      string[] selectionargs, string sortorder) {
    sqlitedatabase db = dbopenhelper.getreadabledatabase();
    switch (urimatcher.match(uri)) {
    case persons:
      return db.query("person", projection, selection, selectionargs, null, null, sortorder);
    case person:
      // 下面的方法用于從uri中解析出id,對這樣的路徑content://com.ljq.provider.personprovider/person/10
      // 進行解析,返回值為10
      long personid = contenturis.parseid(uri);
      string where = "id=" + personid;// 獲取指定id的記錄
      where += !textutils.isempty(selection) ? " and (" + selection + ")" : "";// 把其它條件附加上
      return db.query("person", projection, where, selectionargs, null, null, sortorder);
    default:
      throw new illegalargumentexception("unknown uri " + uri);
    }
  }
}

文件清單

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ljq.sql" android:versioncode="1"
  android:versionname="1.0">
  <application android:icon="@drawable/icon"
    android:label="@string/app_name">
    <uses-library android:name="android.test.runner" />
    <activity android:name=".sqlactivity"
      android:label="@string/app_name">
      <intent-filter>
        <action android:name="android.intent.action.main" />
        <category
          android:name="android.intent.category.launcher" />
      </intent-filter>
    </activity>
    <provider android:name="com.ljq.db.personcontentprovider"
       android:authorities="com.ljq.provider.personprovider" />
  </application>
  <uses-sdk android:minsdkversion="7" />
  <instrumentation
    android:name="android.test.instrumentationtestrunner"
    android:targetpackage="com.ljq.sql" android:label="tests for my app" />
</manifest>

personcontentprovidertest內容提供者測試類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.ljq.test;
import android.content.contentresolver;
import android.content.contentvalues;
import android.database.cursor;
import android.net.uri;
import android.test.androidtestcase;
import android.util.log;
/**
 * 外部訪問內容提供者
 *
 * @author jiqinlin
 *
 */
public class personcontentprovidertest extends androidtestcase{
  private static final string tag = "personcontentprovidertest";
  public void testsave() throws throwable{
    contentresolver contentresolver = this.getcontext().getcontentresolver();
    uri inserturi = uri.parse("content://com.ljq.provider.personprovider/person");
    contentvalues values = new contentvalues();
    values.put("name", "ljq");
    values.put("phone", "1350000009");
    uri uri = contentresolver.insert(inserturi, values);
    log.i(tag, uri.tostring());
  }
  public void testupdate() throws throwable{
    contentresolver contentresolver = this.getcontext().getcontentresolver();
    uri updateuri = uri.parse("content://com.ljq.provider.personprovider/person/1");
    contentvalues values = new contentvalues();
    values.put("name", "linjiqin");
    contentresolver.update(updateuri, values, null, null);
  }
  public void testfind() throws throwable{
    contentresolver contentresolver = this.getcontext().getcontentresolver();
    //uri uri = uri.parse("content://com.ljq.provider.personprovider/person");
    uri uri = uri.parse("content://com.ljq.provider.personprovider/person");
    cursor cursor = contentresolver.query(uri, null, null, null, "id asc");
    while(cursor.movetonext()){
      int personid = cursor.getint(cursor.getcolumnindex("id"));
      string name = cursor.getstring(cursor.getcolumnindex("name"));
      string phone = cursor.getstring(cursor.getcolumnindex("phone"));
      log.i(tag, "personid="+ personid + ",name="+ name+ ",phone="+ phone);
    }
    cursor.close();
  }
  public void testdelete() throws throwable{
    contentresolver contentresolver = this.getcontext().getcontentresolver();
    uri uri = uri.parse("content://com.ljq.provider.personprovider/person/1");
    contentresolver.delete(uri, null, null);
  }
}

希望本文所述對大家android程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本三级欧美三级人妇英文 | 男生的j桶女人屁免费视频 男生操男生 | 欧美怡红院视频一区二区三区 | 亚洲欧美一区二区三区在线观看 | 亚洲va久久久噜噜噜久久狠狠 | 国产精品一区二区在线观看完整版 | 黑白配高清hd在线视频 | 日本一区二区三区国产 | www.av色| 五月激激激综合网色播免费 | 久久国产主播福利在线 | 午夜一级视频 | 欧美一级一级做性视频 | 三级黄片毛片 | 乌克兰粉嫩摘花第一次 | 操国产美女 | 青青草国产免费久久久91 | 国产精品亚洲精品观看不卡 | 奇米影视77777 | 暖暖日本高清 | 国产精品合集久久久久青苹果 | 国产自拍资源 | 日本国产最新一区二区三区 | 男人最爱看的网站 | 日韩不卡一区二区 | 天堂漫画破解版 | 国产成人一区二区三区在线视频 | 69pao强力打造免费高速 | 国产精品免费小视频 | 亚洲国产精品成人午夜在线观看 | 白丝美女用胸伺候主人 | 四虎影业 | 国产手机在线αⅴ片无码观看 | 国产小视频网站 | 日韩精品一区二区三区中文字幕 | 洗濯屋动漫在线观看 | 成人国产精品一区二区不卡 | 国产精品成人麻豆专区 | 国产a一级毛片爽爽影院 | 亚洲国产五月综合网 | 奇米色88欧美一区二区 |