1

我在使用内容解析器从 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);

    }
}
4

1 回答 1

1

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.
于 2015-01-26T21:00:51.427 回答