【问题标题】:access user dictionary android访问用户字典android
【发布时间】:2012-12-06 13:37:33
【问题描述】:

您好,这是我从用户词典中获取单词的代码。这是我的代码,但我无法运行它......

private String getwordlist() {

        String[] mSelectionArgs={""};


    String[] mProjection ={UserDictionary.Words._ID, UserDictionary.Words.WORD, UserDictionary.Words.FREQUENCY};
    String mSelectionClause = null;

    String mSortOrder = null;

     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


     if (mCursor.moveToFirst()) {            
            do {             
               String id = mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words._ID));          
               String word = mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words.WORD));
               String freq= mCursor.getString(mCursor.getColumnIndex(UserDictionary.Words.FREQUENCY));
               str=str+" id: "+id+"  word: "+"  frequency: "+freq+"\n";
               System.out.println(str);
            } while(mCursor.moveToNext());
            return str;
     }
     return null;        
}

我遇到了错误

12-06 18:49:58.586: E/AndroidRuntime(17174): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.ScrollView}: java.lang.IllegalArgumentException: Cannot bind argument at index 1 because the index is out of range.  The statement has 0 parameters.

这是什么错误..

【问题讨论】:

    标签: android dictionary android-contentprovider


    【解决方案1】:

    似乎当mSelectionClause为null时,mSelectionArgs也需要为null。

    // If the word is the empty string, gets everything
    if (TextUtils.isEmpty(searchWord)) {
        // Setting the selection clause to null will return all words
        mSelectionClause = null;
        mSelectionArgs = null;
    } else {
        // Constructs a selection clause that matches the word that the user entered
        mSelectionClause = UserDictionary.Words.WORD + " = ?";
        // Moves the user's input to the selection arguments
        mSelectionArgs = new String[] {searchWord};
    }
    
    // Does a query against the table and returns a Cursor object
    Cursor cursor = 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
        null);
    

    【讨论】:

    【解决方案2】:

    我认为您的 do while 循环存在缺陷。 Content Provider (http://developer.android.com/guide/topics/providers/content-provider-basics.html#AltForms) 的开发者文档提供了以下示例代码:

    /*
     * 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.
    }
    

    你能尝试用这个逻辑重写你的代码吗?

    【讨论】:

      猜你喜欢
      • 2017-11-14
      • 2020-01-16
      • 1970-01-01
      • 2020-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-16
      • 1970-01-01
      相关资源
      最近更新 更多