package com.example.drawernavigation.fragment; import com.example.drawernavigation.MainActivity; import com.example.drawernavigation.R; import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.Bundle; import android.provider.UserDictionary; import android.support.v4.app.Fragment; import android.support.v4.widget.SimpleCursorAdapter; import android.text.TextUtils; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class BFragmentDrawer extends Fragment { MainActivity.MyOnTouchListener myOnTouchListener; private static final String TAG = "BFragmentDrawer"; private Button content_provider_bt, content_provider_insertbt; private EditText content_provider_edt; ListView mWordList; String mSearchString; SimpleCursorAdapter mCursorAdapter; Cursor mCursor; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_b, container, false); content_provider_edt = (EditText) view .findViewById(R.id.content_provider_edt); content_provider_bt = (Button) view .findViewById(R.id.content_provider_bt); content_provider_bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { querySearch(); } }); content_provider_insertbt = (Button) view .findViewById(R.id.content_provider_insertbt); content_provider_insertbt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { insertSearch(); } }); mWordList = (ListView) view.findViewById(R.id.mWordList); return view; } public void insertSearch() { // 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.user2"); mNewValues.put(UserDictionary.Words.LOCALE, "en_US"); mNewValues.put(UserDictionary.Words.WORD, "insert2"); mNewValues.put(UserDictionary.Words.FREQUENCY, "100"); mNewUri = getActivity().getContentResolver().insert( UserDictionary.Words.CONTENT_URI, // the user dictionary content // URI mNewValues // the values to insert ); } public void querySearch() { // a "projection" 定義了要返回的行的columns String[] mProjection = { UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.LOCALE }; // 定義一個string 來包含 要選擇的分句 String mSelectionClause = null; // 初始化一個array 來包含 要選擇的參數 String[] mSelectionArgs = { "" }; // 從UI線程獲得一個單詞 mSearchString = content_provider_edt.getText().toString(); // 插入代碼,檢查 輸入的有效性和安全性 /** * 如果是空,返回所有值 */ if (TextUtils.isEmpty(mSearchString)) { Log.e(TAG, "輸入為空"); 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(); } }