找呀志_ContentResolver操作ContentProvider数据
當需要外部的應用ContentProvider該數據被添加、刪、修改和查詢操作。可以使用ContentResolver 類完成
要得到ContentResolver 物,可以使用Activity提供getContentResolver()辦法。
?ContentResolver 類提供了與ContentProvider類同樣簽名的四個方法:
這些方法的第一個參數為Uri,代表要操作的是哪個ContentProvider和對當中的什么數據進行操作。如果給定的是
Uri.parse(“content://cn.itcast.provider.personprovider/person/10”)那么將會對主機名為cn.itcast.provider.personprovider的ContentProvider進行操作。操作的數據為person表中id為10的記錄。
使用ContentResolver對ContentProvider中的數據進行加入、刪除、改動和查詢操作:
MainActivity.java package com.example.android_provider;import android.app.Activity; import android.content.ContentResolver; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.support.v4.widget.CursorAdapter; import android.support.v4.widget.SimpleCursorAdapter; import android.widget.ListView;public class MainActivity extends Activity {private ListView lv_user;// 內容解析器private ContentResolver contentResolver;// 操作的標志private static final String AUTHORITIE = "www.csdn.com.provider.userContentProvider";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 模型層// 創建內容解析器對象contentResolver = getContentResolver();// 控制層Cursor c = contentResolver.query(Uri.parse("content://" + AUTHORITIE + "/query"), new String[] {"userid as _id", "username", "userphone" }, null, null,null);SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.list_item_user, c, new String[] { "username","userphone" },new int[] { R.id.tv_name, R.id.tv_phone },CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);// 視圖層lv_user = (ListView) findViewById(R.id.listView1);lv_user.setAdapter(adapter);}} activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><ListViewandroid:id="@+id/listView1"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbarStyle="outsideInset"></ListView></RelativeLayout>
list_item_user.xml <?xml version="1.0" encoding="utf-8"?
> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" > ? ? <Button ? ? ? ? android:id="@+id/btn_delete" ? ? ? ? android:layout_width="30dp" ? ? ? ? android:layout_height="30dp" ? ? ? ? android:layout_alignParentRight="true" ? ? ? ? android:layout_alignParentTop="true" ? ? ? ? android:layout_marginTop="5dp" ? ? ? ? android:layout_marginLeft="5dp" ? ? ? ? android:layout_marginRight="10dp" ? ? ? ? android:background="@drawable/del" ? ? ? ? android:onClick="delete" /> ? ? <Button ? ? ? ? android:id="@+id/btn_update" ? ? ? ? android:layout_width="30dp" ? ? ? ? android:layout_height="30dp" ? ? ? ? android:layout_alignParentTop="true" ? ? ? ? android:layout_toLeftOf="@+id/btn_delete" ? ? ? ? android:onClick="update" ? ? ? ?android:layout_margin="5dp" ? ? ? ? ? android:background="@drawable/refresh" /> ? ? <TextView ? ? ? ? android:id="@+id/tv_name" ? ? ? ? android:layout_width="60dp" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignBaseline="@+id/btn_update" ? ? ? ? android:layout_alignBottom="@+id/btn_update" ? ? ? ? android:layout_alignParentLeft="true" ? ? ? ? android:paddingLeft="10dp" ? ? ? ? android:text="TextView"? ? ? ? ? /> ? ? <TextView ? ? ? ? android:id="@+id/tv_phone" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_alignBaseline="@+id/tv_name" ? ? ? ? android:layout_toRightOf="@+id/tv_name" ? ? ? ? android:paddingLeft="10dp" ? ? ? ? android:text="TextView" /> </RelativeLayout>
版權聲明:本文博主原創文章。博客,未經同意不得轉載。
總結
以上是生活随笔為你收集整理的找呀志_ContentResolver操作ContentProvider数据的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 华为新品路由器A2评测 华为路由器A2评
- 下一篇: Yii Framework2.0开发教程