簡體   English   中英

如何從用戶詞典內容提供商檢索單詞

[英]How can I retrieve words from userdictionary content provider

我在使用內容解析器從UserDictionary.words內容提供程序獲取數據時遇到問題。

這是我的要求:1.我想找出與給定單詞匹配的UserDictionary單詞。

我的實現; 1.我的應用有兩個屏幕(活動)2.我在第一個屏幕上輸入了一個字,然后將此作為意圖傳遞給第二個屏幕。 在第二個屏幕上,我想在第一個屏幕上顯示與給定單詞匹配的Userdictionary單詞列表。

我的問題是...我能夠將單詞傳遞給第二個活動,但是錯過了檢索userDicitnary記錄的機會。 我給它的單詞無論返回的記錄。 這是我兩項活動的代碼。 請幫助我解決問題。

第一次活動:

public void onCreate(Bundle savedInstanceState) {

    Log.d(TAG,"onCreate");
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_content_user_demo);
}

public void SendWord(View view){
    Log.d(TAG,"SendWord");
    EditText SearchWord = (EditText) findViewById(R.id.mSearchWord);
    String mSearchString = SearchWord.getText().toString();    
    Intent intent = new Intent(this, WordListActivity.class);
    Log.d(TAG,"EXTRA_MESSAGE");
    intent.putExtra(EXTRA_MESSAGE, mSearchString);
    startActivity(intent);
}

第二項活動:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG,"onCreate");
    setContentView(R.layout.activity_word_list);
    Intent intent = getIntent();
    Log.d(TAG,"getIntent() EXTRA_MESSAGE");

    String searchWord = intent.getStringExtra(ContentUserDemo.EXTRA_MESSAGE);
   textView = new TextView(this);
    Log.d(TAG,"searchWord " +searchWord);
    textView.setTextSize(20);
    setContentView(textView);
    Log.d(TAG,"textSize " +textView.length());
       String[] mProjection =
        {
            UserDictionary.Words._ID,    
            UserDictionary.Words.WORD,                                                                      
            UserDictionary.Words.LOCALE  
        };
    String mSelectionClause = null;
    String[] mSelectionArgs = {""};
    String mSortOrder=null;
    ContentResolver cr = getContentResolver();
    if (TextUtils.isEmpty(searchWord)) {
        mSelectionClause = null;
        mSelectionArgs[0] = "";
    } 
    else {       
        mSelectionClause = UserDictionary.Words.WORD + " = ?";
        mSelectionArgs[0] = searchWord;
    }
    Cursor mCursor = cr.query(UserDictionary.Words.CONTENT_URI,
            mProjection,
            mSelectionClause, 
            mSelectionArgs,
            mSortOrder);
    startManagingCursor(mCursor);


    int count = mCursor.getCount();


    if (null == mCursor) {
       Log.d(TAG, "cursor.getCount()=" + count);
        String message="No word matches with "+ searchWord;
        textView.setText(message);
    } 
    else if (mCursor.getCount() < 1) {
       Log.d(TAG, "cursor.getCount()=" + count);
        String message="getCount is less than 1 " + "No word matches with "+ searchWord;
        textView.setText(message);

    } 
    else {
        String message="Else part of the code";
        textView.setText(message);
          String[] mWordListColumns ={UserDictionary.Words.WORD,UserDictionary.Words.LOCALE};
          int[] mWordListItems = { R.id.dictWord, R.id.locale};
          SimpleCursorAdapter mCursorAdapter = new SimpleCursorAdapter(
                getApplicationContext(),               
                R.layout.wordlistrow,                  
                mCursor,                               
                mWordListColumns,                      
                mWordListItems,                        
                0); 
          ListView mWordList = (ListView) findViewById(R.id.mWordList);
          mWordList.setAdapter(mCursorAdapter);

    }
}

TextUtils.isEmpty() {}子句中,我建議更改:

mSelectionArgs[0] = ""; mSelectionArgs = null;

由於可能存在非法參數異常:

java.lang.IllegalArgumentException: 
Cannot bind argument at index 1 because the index is out of range. 
The statement has 0 parameters.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM