标签:
1 package com.example.day08_contentvisitor; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.database.Cursor; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.util.Log; 9 import android.view.Menu; 10 import android.view.MenuItem; 11 import android.view.View; 12 13 public class MainActivity extends Activity { 14 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 } 20 21 public void delete(View v){ 22 getContentResolver().delete(Uri.parse("content://com.example.mycontentprovider"), "id=?", new String[]{"1"}); 23 } 24 public void update(View v){ 25 ContentValues c2 = new ContentValues(); 26 c2.put("name", "zhaosi"); 27 c2.put("telephone", "18888888888"); 28 getContentResolver().update(Uri.parse("content://com.example.mycontentprovider"), c2, "id=?", new String[]{"4"}); 29 } 30 public void insert(View v){ 31 ContentValues c = new ContentValues(); 32 c.put("id", 1); 33 c.put("name", "zhangsan"); 34 c.put("telephone", "13899999999"); 35 getContentResolver().insert(Uri.parse( "content://com.example.mycontentprovider"), c); 36 } 37 public void insert2(View v){ 38 ContentValues c = new ContentValues(); 39 c.put("id", 1); 40 c.put("name", "zhangsan"); 41 c.put("wechat", "zhansanfeng"); 42 43 getContentResolver().insert(Uri.parse( "content://com.example.mycontentprovider/userwechat"), c); 44 } 45 46 public void query(View v){ 47 Cursor c= getContentResolver().query(Uri.parse( "content://com.example.mycontentprovider/userphonenum"), 48 new String[]{"id","name" ,"telephone"}, null,null, null); 49 50 while (c.moveToNext()) { 51 int id = c.getInt(0); 52 String name = c.getString(1); 53 String number = c.getString(2); 54 55 Log.i("mycontentvisitor", id+","+name+","+number); 56 } 57 } 58 59 public void query2(View v){ 60 61 Cursor c= getContentResolver().query(Uri.parse( "content://com.cskaoyan.mycontentprovider/userwechat/4"), 62 new String[]{"id","name" ,"wechat"}, null,null, null); 63 64 while (c.moveToNext()) { 65 int id = c.getInt(0); 66 String name = c.getString(1); 67 String number = c.getString(2); 68 69 Log.i("mycontentvisitor", id+","+name+","+number); 70 } 71 } 72 73 public void query3(View v){ 74 75 Cursor c= getContentResolver().query(Uri.parse( "content://com.cskaoyan.mycontentprovider/userwechat/wechat"), 76 null, null,null, null); 77 78 while (c.moveToNext()) { 79 String wechat = c.getString(0); 80 Log.i("mycontentvisitor", wechat); 81 } 82 83 } 84 }
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 android:paddingLeft="@dimen/activity_horizontal_margin" 8 android:paddingRight="@dimen/activity_horizontal_margin" 9 android:paddingTop="@dimen/activity_vertical_margin" 10 tools:context="com.example.mycontentvisitor.MainActivity" > 11 12 <TextView 13 android:layout_width="wrap_content" 14 android:layout_height="wrap_content" 15 android:text="@string/hello_world" /> 16 17 <Button 18 android:layout_width="fill_parent" 19 android:layout_height="wrap_content" 20 android:onClick="delete" 21 android:text="delete" /> 22 23 <Button 24 android:layout_width="fill_parent" 25 android:layout_height="wrap_content" 26 android:onClick="insert" 27 android:text="insert" /> 28 29 <Button 30 android:layout_width="fill_parent" 31 android:layout_height="wrap_content" 32 android:onClick="update" 33 android:text="update" /> 34 35 <Button 36 android:layout_width="fill_parent" 37 android:layout_height="wrap_content" 38 android:onClick="query" 39 android:text="query" /> 40 41 <Button 42 android:layout_width="fill_parent" 43 android:layout_height="wrap_content" 44 android:onClick="insert2" 45 android:text="insertinto wechat" /> 46 47 48 <Button 49 android:layout_width="fill_parent" 50 android:layout_height="wrap_content" 51 android:onClick="query2" 52 android:text="query one from wechat" /> 53 54 55 <Button 56 android:layout_width="fill_parent" 57 android:layout_height="wrap_content" 58 android:onClick="query3" 59 android:text="query name from wechat" /> 60 </LinearLayout>
标签:
原文地址:http://www.cnblogs.com/woodrow2015/p/4513252.html