android - 如何从 userdictionary content provider 检索单词

标签 android android-contentprovider

我在使用内容解析器从 UserDictionary.words 内容提供程序获取数据时遇到问题。

这是我的要求: 1. 我想找出与给定单词匹配的 UserDictionary 单词。

我的实现; 1.我的应用有两个屏幕( Activity ) 2. 我在第一个屏幕上输入一个词并将其作为 Intent 传递给第二个屏幕。 在第二个屏幕上,我想显示与第一个屏幕上给定单词匹配的 Userdictionary 单词列表。

我的问题是...我能够将单词传递给第二个 Activity ,但错过了检索 userDicitnary 记录的机会。我给它的任何词都是返回记录。这是我的两个 Activity 的代码。请帮我解决这个问题。

第一个 Activity :

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);
}

第二个 Activity :

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.

关于android - 如何从 userdictionary content provider 检索单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338943/

相关文章:

android - 短信被复制为通话(三星 galaxy S II)

android - 将图像从 ListView 加载到下一个 Activity

android - Android中SharedPreferences的加密

java - 随机按钮Android项目

android - 可滚动的 TextView 文本变黑

android - ContentProvider 数据库未被删除

Android 依赖在编译和运行时有不同的版本

android - 在多个 Android 应用程序之间共享和持久化数据

android 获取联系人详细信息,例如号码和缩略图

android - 我需要一个完整的 promise 内容提供者吗?