Android-Content Provide
2015-12-30 11:45
393 查看
内容提供者 是为了不同的应用程序共享数据的
主要有以下几个方面 1.为其他程序提供数据 2.获取其他程序提供的数据
类似于服务器客户端这样的程序 提供ContentProvider的是服务器 接收ContentProvider的是客户端
一.如何获得ContentProvider的数据
看如下的几行代码
客户端获取一个ContentResolver的类 然后调用其增删该查方法获取数据 返回一个Cursor的数据
参数理解如下
URI:唯一的标识 应该和服务器端的URI相同
比如这样的形式:
其中 content://是固定的 后面的服务器约定的字段 一般后面是表明
Projection:投影 学过数据库的都知道 投影就是挑选某几列 这是一个数组
SelectionClause:选择的添加 相当于数据库查询语句的where字句的条件
SelectionArgs:是SelectionClause的占位符?所代表的参数
SortOrder:按照什么排序
比如如下代码:
/*
* This defines a one-element String array to contain the selection argument.
*/
String[] mSelectionArgs = {""};
// Gets a word from the UI
mSearchString = mSearchWord.getText().toString();
// Remember to insert code here to check for invalid or malicious input.
// If the word is the empty string, gets everything
if (TextUtils.isEmpty(mSearchString)) {
// Setting the selection clause to null will return all words
mSelectionClause = null;
mSelectionArgs[0] = "";
} else {
// Constructs a selection clause that matches the word that the user entered.
mSelectionClause = UserDictionary.Words.WORD + " = ?";
// Moves the user's input string to the selection arguments.
mSelectionArgs[0] = mSearchString;
}
// Does a query against the table and returns a Cursor object
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Either null, or the word the user entered
mSelectionArgs, // Either empty, or the string the user entered
mSortOrder); // The sort order for the returned rows
// Some providers return null if an error occurs, others throw an exception
if (null == mCursor) {
/*
* Insert code here to handle the error. Be sure not to use the cursor! You may want to
* call android.util.Log.e() to log this error.
*
*/
// If the Cursor is empty, the provider found no matches
} else if (mCursor.getCount() < 1) {
/*
* Insert code here to notify the user that the search was unsuccessful. This isn't necessarily
* an error. You may want to offer the user the option to insert a new row, or re-type the
* search term.
*/
} else {
// Insert code here to do something with the results
}
接下来如何显示查询出来的Cursor结果呢
1.通过Adapter
2.或者是通过一个While循环
// Determine the column index of the column named "word"
int index = mCursor.getColumnIndex(UserDictionary.Words.WORD);
/*
* Only executes if the cursor is valid. The User Dictionary Provider returns null if
* an internal error occurs. Other providers may throw an Exception instead of returning null.
*/
if (mCursor != null) {
/*
* Moves to the next row in the cursor. Before the first movement in the cursor, the
* "row pointer" is -1, and if you try to retrieve data at that position you will get an
* exception.
*/
while (mCursor.moveToNext()) {
// Gets the value from the column.
newWord = mCursor.getString(index);
// Insert code here to process the retrieved word.
...
// end of while loop
}
} else {
// Insert code here to report an error if the cursor is null or the provider threw an exception.
}增删该查如下:
然后返回的是插入的数据的id
主要有以下几个方面 1.为其他程序提供数据 2.获取其他程序提供的数据
类似于服务器客户端这样的程序 提供ContentProvider的是服务器 接收ContentProvider的是客户端
一.如何获得ContentProvider的数据
看如下的几行代码
// Queries the user dictionary and returns results mCursor = getContentResolver().query( UserDictionary.Words.CONTENT_URI, // The content URI of the words table mProjection, // The columns to return for each row mSelectionClause // Selection criteria mSelectionArgs, // Selection criteria mSortOrder); // The sort order for the returned rows
客户端获取一个ContentResolver的类 然后调用其增删该查方法获取数据 返回一个Cursor的数据
参数理解如下
URI:唯一的标识 应该和服务器端的URI相同
比如这样的形式:
content://user_dictionary/words
其中 content://是固定的 后面的服务器约定的字段 一般后面是表明
Projection:投影 学过数据库的都知道 投影就是挑选某几列 这是一个数组
SelectionClause:选择的添加 相当于数据库查询语句的where字句的条件
SelectionArgs:是SelectionClause的占位符?所代表的参数
SortOrder:按照什么排序
比如如下代码:
/*
* This defines a one-element String array to contain the selection argument.
*/
String[] mSelectionArgs = {""};
// Gets a word from the UI
mSearchString = mSearchWord.getText().toString();
// Remember to insert code here to check for invalid or malicious input.
// If the word is the empty string, gets everything
if (TextUtils.isEmpty(mSearchString)) {
// Setting the selection clause to null will return all words
mSelectionClause = null;
mSelectionArgs[0] = "";
} else {
// Constructs a selection clause that matches the word that the user entered.
mSelectionClause = UserDictionary.Words.WORD + " = ?";
// Moves the user's input string to the selection arguments.
mSelectionArgs[0] = mSearchString;
}
// Does a query against the table and returns a Cursor object
mCursor = getContentResolver().query(
UserDictionary.Words.CONTENT_URI, // The content URI of the words table
mProjection, // The columns to return for each row
mSelectionClause // Either null, or the word the user entered
mSelectionArgs, // Either empty, or the string the user entered
mSortOrder); // The sort order for the returned rows
// Some providers return null if an error occurs, others throw an exception
if (null == mCursor) {
/*
* Insert code here to handle the error. Be sure not to use the cursor! You may want to
* call android.util.Log.e() to log this error.
*
*/
// If the Cursor is empty, the provider found no matches
} else if (mCursor.getCount() < 1) {
/*
* Insert code here to notify the user that the search was unsuccessful. This isn't necessarily
* an error. You may want to offer the user the option to insert a new row, or re-type the
* search term.
*/
} else {
// Insert code here to do something with the results
}
接下来如何显示查询出来的Cursor结果呢
1.通过Adapter
// Sets the adapter for the ListView mWordList.setAdapter(mCursorAdapter);
2.或者是通过一个While循环
// Determine the column index of the column named "word"
int index = mCursor.getColumnIndex(UserDictionary.Words.WORD);
/*
* Only executes if the cursor is valid. The User Dictionary Provider returns null if
* an internal error occurs. Other providers may throw an Exception instead of returning null.
*/
if (mCursor != null) {
/*
* Moves to the next row in the cursor. Before the first movement in the cursor, the
* "row pointer" is -1, and if you try to retrieve data at that position you will get an
* exception.
*/
while (mCursor.moveToNext()) {
// Gets the value from the column.
newWord = mCursor.getString(index);
// Insert code here to process the retrieved word.
...
// end of while loop
}
} else {
// Insert code here to report an error if the cursor is null or the provider threw an exception.
}增删该查如下:
Inserting data
// Defines a new Uri object that receives the result of the insertion Uri mNewUri; ... // Defines an object to contain the new values to insert ContentValues mNewValues = new ContentValues(); /* * Sets the values of each column and inserts the word. The arguments to the "put" * method are "column name" and "value" */ mNewValues.put(UserDictionary.Words.APP_ID, "example.user"); mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); mNewValues.put(UserDictionary.Words.WORD, "insert"); mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); mNewUri = getContentResolver().insert( UserDictionary.Word.CONTENT_URI, // the user dictionary content URI mNewValues // the values to insert );
然后返回的是插入的数据的id
Updating data
// Defines an object to contain the updated values ContentValues mUpdateValues = new ContentValues(); // Defines selection criteria for the rows you want to update String mSelectionClause = UserDictionary.Words.LOCALE + "LIKE ?"; String[] mSelectionArgs = {"en_%"}; // Defines a variable to contain the number of updated rows int mRowsUpdated = 0; ... /* * Sets the updated value and updates the selected words. */ mUpdateValues.putNull(UserDictionary.Words.LOCALE); mRowsUpdated = getContentResolver().update( UserDictionary.Words.CONTENT_URI, // the user dictionary content URI mUpdateValues // the columns to update mSelectionClause // the column to select on mSelectionArgs // the value to compare to );
Deleting data
// Defines selection criteria for the rows you want to delete String mSelectionClause = UserDictionary.Words.APP_ID + " LIKE ?"; String[] mSelectionArgs = {"user"}; // Defines a variable to contain the number of rows deleted int mRowsDeleted = 0; ... // Deletes the words that match the selection criteria mRowsDeleted = getContentResolver().delete( UserDictionary.Words.CONTENT_URI, // the user dictionary content URI mSelectionClause // the column to select on mSelectionArgs // the value to compare to );
相关文章推荐
- Android中软解码和硬解码的优先级
- Android弹出窗口中ListView显示内容
- 在WebStorm/Android Studio中GIT修改用户名密码
- Android制作粒子爆炸特效
- android使用官方v4裁剪圆角和圆角矩形
- Android studio导入项目时常见问题
- 使用Gradle管理Debug/Release版本的Key
- android通知栏Notification点击,取消,清除响应事件
- android 粘贴 复制
- android 应用调用QQ交流,类似客服
- Android Studio 快捷键(汇总)
- Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
- android 启动页面的设置
- android不依赖具体activity弹出Dialog对话框,即全局性对话框
- Windows系统下安装 Android Studio 的步骤记录
- Android AudioPolicyService和AudioPolicyManager
- Android studio使用简单的GsonFormat插件
- android 软件键盘
- android 百分比layout布局
- Android中Activity四种启动模式和taskAffinity属性详解