您好,登錄后才能下訂單哦!
public class MyTest extends AndroidTestCase { public MyTest() { // TODO Auto-generated constructor stub } public void insert() { // 訪問內容提供者的步驟: // 1、需要一個內容解析者 ContentResolver contentResolver = getContext().getContentResolver(); // 使用content://+授權路徑 Uri url = Uri .parse("content://com.example.android_contentprovider2.StudentProvider/student"); ContentValues values = new ContentValues(); values.put("name", "王五"); values.put("address", "廣西"); contentResolver.insert(url, values); } public void delete() { ContentResolver contentResolver = getContext().getContentResolver(); // 刪除單行記錄,如果要刪除多行記錄:content://com.example.android_contentprovider2.StudentProvider/student Uri uri = Uri .parse("content://com.example.android_contentprovider2.StudentProvider/student/1"); contentResolver.delete(uri, null, null); } public void update() { ContentResolver contentResolver = getContext().getContentResolver(); Uri uri = Uri .parse("content://com.example.android_contentprovider2.StudentProvider/student/2"); ContentValues values = new ContentValues(); values.put("name", "李斯"); values.put("address", "上海"); contentResolver.update(uri, values, null, null); } public void query() { ContentResolver contentResolver = getContext().getContentResolver(); // 查詢單條記錄:content://com.example.android_contentprovider2.StudentProvider/student/2 // 查詢多條記錄:content://com.example.android_contentprovider2.StudentProvider/student Uri uri = Uri .parse("content://com.example.android_contentprovider2.StudentProvider/student"); // select * from student where id = 2; Cursor cursor = contentResolver.query(uri, null, null, null, null); while (cursor.moveToNext()) { System.out.println("---->>" + cursor.getString(cursor.getColumnIndex("name"))); } } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。