本文介绍了Sqlite_Content提供商助手的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让我的ListView通过内容提供商进行刷新。我创建了我的供应商,并试图将其propelry链接到我SqlLite DB。一旦我有我的供应商完成,我将实现我cursorloader但第一次我需要我的帮助提供商。我还没有找到和如何创建一个内容提供商新手用户的详细信息。任何援助将AP preciated。我的DB和提供如下公布。我的主要问题是试图建立一个URI链接到我的数据库。

DB:

 公共类的数据存储扩展SQLiteOpenHelper {//属性表
公共静态最后弦乐DATABASE_NAME =SiteLogindb;
公共静态最终诠释DATABASE_VERSION = 2;
公共静态最后弦乐TABLE_NAME_INFOTABLE =infoTable;//数据属性
公共静态最后弦乐COLUMN_NAME_SITE =SNAME;
公共静态最后弦乐COLUMN_NAME_ADDRESS =wUrl;
公共静态最后弦乐COLUMN_NAME_USERNAME =的uname;
公共静态最后弦乐COLUMN_NAME_PASSWORD =PWORD;
公共静态最后弦乐COLUMN_NAME_NOTES =lNotes;公共数据存储(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
        // TODO自动生成构造函数存根
    }
    @覆盖
    公共无效的onCreate(SQLiteDatabase DB){
        // TODO自动生成方法存根        字符串sqlDataStore =创建表如果不存在+
        TABLE_NAME_INFOTABLE +(+ BaseColumns._ID +整数主键自动增量                    + COLUMN_NAME_SITE +文本不为空,
                    + COLUMN_NAME_ADDRESS +文本不为空,
                    + COLUMN_NAME_USERNAME +文本不为空,
                    + COLUMN_NAME_PASSWORD +文本不为空,
                    + COLUMN_NAME_NOTES +文字NOT NULL);;        db.execSQL(sqlDataStore);
    }
    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        // TODO自动生成方法存根
        如果(oldVersion == 1和;&放大器; NEWVERSION == 2){
            //升级数据库     }        }
          }

内容提供商:

 公共类ListProvider扩展ContentProvider的{
私人数据存储六味地黄丸;私有静态最后弦乐AUTHORITY =com.loginplus.home.ListProvider;
公共静态最终诠释教程= 1;
公共静态最终诠释TUTORIAL_ID = 2;
私有静态最后弦乐TUTORIALS_BASE_PATH =教程;
公共静态最终乌里CONTENT_URI = Uri.parse(内容://+ AUTHORITY
        +/+ TUTORIALS_BASE_PATH);
公共静态最后弦乐CONTENT_ITEM_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE
        +/吨教程;
公共静态最后弦乐CONTENT_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE
        +/吨教程;@覆盖
公共布尔的onCreate(){
    六味地黄丸=新的数据存储(的getContext());
    返回true;
}私有静态最终UriMatcher sURIMatcher =新UriMatcher(
        UriMatcher.NO_MATCH);
静态的 {
    sURIMatcher.addURI(权威,TUTORIALS_BASE_PATH,教程);
    sURIMatcher.addURI(权威,TUTORIALS_BASE_PATH +/#,TUTORIAL_ID);
}@覆盖
公共光标查询(URI URI,字符串[]投影,字符串的选择,
        的String [] selectionArgs两个,字符串中将sortOrder){
    SQLiteQueryBuilder的QueryBuilder =新SQLiteQueryBuilder();
    queryBuilder.setTables(dataStore.TABLE_NAME_INFOTABLE);
    INT uriType = sURIMatcher.match(URI);
    开关(uriType){
    案例TUTORIAL_ID:
        queryBuilder.appendWhere(dataStore.DATABASE_VERSION +=
                + uri.getLastPathSegment());
        打破;
    案例教程:
        // 没有过滤器
        打破;
    默认:
        抛出新抛出:IllegalArgumentException(未知URI);
    }
    光标光标= queryBuilder.query(lDB.getReadableDatabase()
            投影,选择,selectionArgs两个,NULL,NULL,中将sortOrder);
    cursor.setNotificationUri(的getContext()getContentResolver(),URI);
    返回游标;
}
//删除DB项
@覆盖
公众诠释删除(URI URI,选择字符串,字符串[] selectionArgs两个){
    INT uriType = sURIMatcher.match(URI);
    SQLiteDatabase SQLDB = lDB.getWritableDatabase();
    INT的RowsAffected = 0;
    开关(uriType){
    案例教程:
        的RowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                选择selectionArgs两个);
        打破;
    案例TUTORIAL_ID:
        字符串ID = uri.getLastPathSegment();
        如果(TextUtils.isEmpty(选择)){
            的RowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                    BaseColums.ID +=+编号,零);
        }其他{
            的RowsAffected = sqlDB.delete(dataStore.TABLE_NAME_INFOTABLE,
                    选择+和+ BaseColums.ID +=+编号,
                    selectionArgs两个);
        }
        打破;
    默认:
        抛出新抛出:IllegalArgumentException(未知或无效的URI+ URI);
    }
    的getContext()getContentResolver()有NotifyChange(URI,空)。
    返回的RowsAffected;
}
@覆盖
公共字符串的getType(URI URI){
    // TODO自动生成方法存根
    返回null;
}
//插入数据库条目
@覆盖
公共乌里插入(URI URI,ContentValues​​值){
    // TODO自动生成方法存根
            返回null;
}
//更新数据库条目
@覆盖
公众诠释更新(开放的URI,ContentValues​​值,字符串的选择,
        的String [] selectionArgs两个){
    INT uriType = sURIMatcher.match(URI);
    SQLiteDatabase SQLDB = lDB.getWritableDatabase();
    INT的RowsAffected = 0;
    开关(uriType){
    案例教程:
        的RowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                空,选择selectionArgs两个);
        打破;
    案例TUTORIAL_ID:
        字符串ID = uri.getLastPathSegment();
        如果(TextUtils.isEmpty(选择)){
            的RowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                    空,BaseColums.ID +=+ ID,NULL);
        }其他{
            的RowsAffected = sqlDB.update(dataStore.TABLE_NAME_INFOTABLE,
                    空,选择+和+ BaseColums.ID +=+编号,
                    selectionArgs两个);
        }
        打破;
    默认:
        抛出新抛出:IllegalArgumentException(未知或无效的URI+ URI);
    }
    的getContext()getContentResolver()有NotifyChange(URI,空)。
    返回的RowsAffected;
}
  }


解决方案

  • Right now your ListProvider.CONTENT_URI will match in your ListProvider using your UriMatcher. And your ListProvider is using your dataStore.TABLE_NAME_INFOTABLE which is the only table in your database.

  • So you can already use your ContentProvider directly by using a ContentResolver in your Activity with code like this:

     getContentResolver().query(ListProvider.CONTENT_URI, projection, selection, selectionArgs, sortOrder);
     getContentResolver().delete(ListProvider.CONTENT_URI, where, selectionArgs);
     //etc...
    


  • Your ContentProvider appears to be done. However, you must remember to add it to your manifest with the same AUTHORITY. You can rename the authority, but it must be unique and match in your `ContentProvider and your manifest.

    <provider
        android:name="com.loginplus.home.ListProvider"
        android:authorities="com.loginplus.home.ListProvider" >
    </provider>
    

  • You will then be able to create your CursorLoader in the public Loader<Cursor> onCreateLoader(int id, Bundle args) callback.

    CursorLoader cursorLoader = new CursorLoader(this, ListProvider.CONTENT_URI, projection, selection, selectionArgs, orderBy);
    


  • Lars Vogella has a great tutorial that walks you through creating your own SQLite database, ContentProvider, and CursorLoader. This helped me get started and gave me a great basis.

这篇关于Sqlite_Content提供商助手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 01:05