zoukankan      html  css  js  c++  java
  • 访问内容提供者(和上文联系),测试

    内容提供者提供了对自定义的一个SQLite数据库的表person中增删改查,对象为person(bean)

    public class TestPersonContentProvider extends AndroidTestCase
    {

      private static final String TAG = "TestPersonContentProvider";

      public void testInsert()
      {
        //访问地址:content://要访问的包名.类名/person/insert

        ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类

        ContentValues values = new ContentValues();
        values.put("name", "sz");
        values.put("age", "20");

        resolver.insert(Uri.parse("content://要访问的包名.类名/person/insert"), values);
      }

      public void testUpdate()
      {
        ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类

        ContentValues values = new ContentValues();

        values.put("name", "1505");
        String where = "id = ?";
        String[] selectionArgs = new String[]{"1"};

        int id = resolver.update(
        Uri.parse("content://要访问的包名.类名/person/update"),
        values ,
        where,
        selectionArgs);
        Log.d(TAG, "UPDATE .... = " + id);
      }

      public void testQuery()
      {
        ContentResolver resolver = getContext().getContentResolver(); //内容提供者的访问类

        //content://要访问的包名.类名/person/query
        String[] projection = new String[]{"id","name","age"};

        Cursor cursor = resolver.query(
        Uri.parse("content://要访问的包名.类名/person/query"),
        projection,
        null,
        null,
        null);

        if(null != cursor && cursor.getCount() > 0)
        {
          while(cursor.moveToNext())
          {
            Integer id = cursor.getInt(0);
            String name = cursor.getString(1);
            Integer age = cursor.getInt(2);

            Log.d(TAG, "id=" + id + " name= " + name + " age= " + age);
          }

          cursor.close(); // 一定记住不要忘了关闭游标
        }
      }

    }

    注意权限:与内容提供者中想对应的权限

    <uses-permission android:name="aaa.bbb.ccc"/>
    <uses-permission android:name="aaa.bbb.ccc.ddd"/>

  • 相关阅读:
    购买云主机时应该注意哪些事项
    wdcp的安装方法与常见问题
    推荐一些不错的计算机书籍(php c mysql linux等等)
    .htaccess详解及.htaccess参数说明【转】
    常用的7个.htaccess代码组织某个国家的IP访问
    最新ecshop v2.7.3版本去版权完全版
    javascript常用数组算法总结
    10道javascript笔试题
    Magento显示多货币,Magento 多货币设置
    Magento后台手动修改订单状态方法及手动修改方法php
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4910307.html
Copyright © 2011-2022 走看看