zoukankan      html  css  js  c++  java
  • 使用ContentProContentProvider共享生词本数据

    自定义ContentProvider需要在项目清单中注册:

    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.provider.UserDictionary.Words;

    public class DictProvider extends ContentProvider{
      private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
      private static final int WORDS = 1;
      private static final int WORD = 2;
      private MyDatabaseHelper dbOpenHelper;
      static{
        //为UriMatcher注册两个Uri
        matcher.addURI(mediaprovidertest.Words.AUTHORITY, "words", WORDS);
        matcher.addURI(mediaprovidertest.Words.AUTHORITY, "word/#", WORD);
      }

      //第一次调用该DictProvider时,系统先创建DictProvider对象,并回调该方法
      @Override
      public boolean onCreate() {
        dbOpenHelper = new MyDatabaseHelper(this.getContext(),"myDict.db3",null,1);
        return true;
      }

      //查询数据的方法
      @Override
      public Cursor query(Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        switch (matcher.match(uri)) {
          case WORDS:
            //执行查询
            return db.query("dict", projection,
                selection, selectionArgs, null, null, sortOrder);
          case WORD:
            //解析出想查询的记录ID
            long id = ContentUris.parseId(uri);
            String where =mediaprovidertest.Words.Word._ID+"="+id;
            //如果原来的where子句存在,拼接where子句
            if(selection != null && "".equals(selection)){
              where = where + " and "+selection;
            }
            return db.query("dict", projection, where, selectionArgs,
                    null, null, sortOrder);

          default:
            throw new IllegalArgumentException("未知Uri:"+uri);
         }
      }

      //返回指定Uri参数对应的数据的MIME类型
      @Override
      public String getType(Uri uri) {
        switch (matcher.match(uri)) {
          //如果操作的数据是多项记录
          case WORDS:
            return "vnd.android.cursor.dir/org.crazyit.dict";
          //如果操作的数据时单项记录
          case WORD:
            return "vnd.android.cursor.item/org.crazyit.dict";
          default:
            throw new IllegalArgumentException("未知Uri:"+uri);
        }
      }

      //插入数据
      @Override
      public Uri insert(Uri uri, ContentValues values) {
        // 获得数据库实例
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        //插入数据,返回行ID
        long rowId = db.insert("dict", mediaprovidertest.Words.Word._ID, values);
        //如果插入成功则返回uri
        if(rowId>0){
          //在已有的Uri的后面追加ID数据
          Uri wordUri = ContentUris.withAppendedId(uri, rowId);
          //通知数据已经改变
          getContext().getContentResolver().notifyChange(wordUri, null);
          return wordUri;
        }
        return null;
      }

      //删除数据的方法
      @Override
      public int delete(Uri uri, String selection, String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
        //记录所删除的记录数
        int num = 0;
        //对Uri进行匹配
        switch (matcher.match(uri)) {
          case WORDS:
            num = db.delete("dict", selection, selectionArgs);
            break;
          case WORD:
            //解析出所需要删除的记录ID
            long id = ContentUris.parseId(uri);
            String where = mediaprovidertest.Words.Word._ID+"="+id;
            //如果原来的where子句存在,拼接where子句
            if(selection != null && selection.equals("")){
              where = where +" and "+selection;
            }
            num = db.delete("dict", where, selectionArgs);
            break;
        default:
          try {
            throw new IllegalAccessException("未知Uri:"+uri);
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }
        }
        //通知数据已经改变
        return num;
      }

      //修改数据的方法
      @Override
      public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        SQLiteDatabase db = dbOpenHelper.getWriteDatabase();
        //记录所修改的记录数
        int num = 0;
        switch (matcher.match(uri)) {
          case WORDS:
            num = db.update("dict", values, selection, selectionArgs);
            break;
          case WORD:
            //解析出想修改的记录ID
            long id = ContentUris.parseId(uri);
            String where = mediaprovidertest.Words.Word._ID+"="+id;
            //如果原来的where子句存在,拼接where子句
            if(selection != null && selection.equals("")){
              where = where +" and "+selection;
            }
            num = db.update("dict", values, where, selectionArgs);
          default:
            throw new IllegalArgumentException("未知Uri:"+uri);
         }
        //通知数据已经改变
        getContext().getContentResolver().notifyChange(uri, null);
          return 0;
       }

    }

  • 相关阅读:
    boost.numpy编译报错:undefined reference to `PyInt_FromLong' libboost_numpy.so: undefined reference to `PyCObject_AsVoidPtr'
    Could not find the following Boost libraries: boost_python3
    mxnet安装
    win7和Ubuntu双系统折腾记
    DBTest/1.TestWrite fails: MDB_BAD_TXN: Transaction cannot recover
    Permission denied:multiarray.cp35-win_amd64.pyd(tensorflow0.12.0在windows下安装)
    mxnet实战系列(一)入门与跑mnist数据集
    pgm revert转换 成jpg 人脸识别图片
    [jv-convert] Error 1,[all-recursive] Error 1
    make: g77: Command not found
  • 原文地址:https://www.cnblogs.com/jiww/p/5595919.html
Copyright © 2011-2022 走看看