Content Provider(一) basics(二)
输入为空");
mSelectionClause = null;
mSelectionArgs[0] = "";
} else {
Log.e(TAG, "输入不为空");
// 构造一个选择分句来匹配用户输入的单词
mSelectionClause = UserDictionary.Words.WORD + " = ";
// 将用户的输入string 来作为选择参数
mSelectionArgs[0] = mSearchString;
}
String mSortOrder = " word ASC";
/**
* 对这个 table 进行查询,返回一个cursor对象
*/
mCursor = getActivity().getContentResolver().query(
UserDictionary.Words.CONTENT_URI, mProjection,
mSelectionClause, mSelectionArgs, null);
Log.i(TAG, UserDictionary.Words.CONTENT_URI.toString());
/**
* 处理返回结果,null, empty,...
*/
if (null == mCursor) {
Log.e(TAG, "结果为null");
/*
* 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.
*/
} 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.
*/
Log.i(TAG, "结果" + mCursor.getCount());
} else {
/**
* 定义simpleCursorAdapter,展示数据
*/
String[] mWordListColumns = { UserDictionary.Words.WORD,
UserDictionary.Words.LOCALE };
int[] mWordListItems = { R.id.dictWord, R.id.locale };
mCursorAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.wordlistrow, mCursor, mWordListColumns,
mWordListItems, 0);
mWordList.setAdapter(mCursorAdapter);
}
/**
* 从查询结果中获得数据
*/
// 找到列“word”的索引
int index = mCursor.getColumnIndex(UserDictionary.Words.WORD);
Log.i(TAG, "word的位置" + index);
// 仅当cursor有效时执行
String newWord;
if (mCursor != null) {
Log.e(TAG, "mCursor不为空");
/*
* 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.
Log.i(TAG, newWord);
// end of while loop
}
} else {
// Insert code here to report an error if the cursor is null or the
// provider threw an exception.
Log.e(TAG, "没有找到word列");
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.e(TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e(TAG, "onCreate");
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.e(TAG, "onActivityCreated");
}
@Override
public void onStart() {
super.onStart();
Log.e(TAG, "onStart");
}
@Override
public void onResume() {
Log.e(TAG, "onResume");
super.onResume();
}
@Override
public void onPause() {
Log.e(TAG, "onPause");
super.onPause();
}
@Override
public void onStop() {
Log.e(TAG, "onStop");
super.onStop();
}
@Override
public void onDestroyView() {
Log.e(TAG, "onDestroyView");
super.onDestroyView();
}
@Override
public void onDestroy() {
Log.e(TAG, "onDestroy");
super.onDestroy();
}
@Override
public void onDetach() {
Log.e(TAG, "onDetach");
super.onDetach();
}
}