我只是學習內容提供商和解析器在Android和試圖使一個簡單的應用程序來打印UserDataDictionary, 的細節,但是,雖然我有很多項目的光標數顯示爲只打印一個項目, 我去了,清除從字典中的所有單詞,但它仍然顯示了一個項目,我剛學的Android,任何幫助,將不勝感激一個和版畫,看到的OnCreate(0以下方法遊標只獲取UserDataDictionary中的最後一項?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Get and display the data dictionary
TextView dictionaryTextView=(TextView)findViewById(R.id.dictionary_text_view);
ContentResolver resolver=getContentResolver();
//get cursor containing all words
Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
String userDictString;
try {
//get total words
int numWords = cursor.getCount();
//get all column Indexes
int columnIndex = cursor.getColumnIndex(UserDictionary.Words._ID);
int columnWords = cursor.getColumnIndex(UserDictionary.Words.WORD);
int columnFrequency = cursor.getColumnIndex(UserDictionary.Words.FREQUENCY);
userDictString=("The User dictionary Contains\n" +cursor.getCount()+" words\n"+UserDictionary.Words._ID +"\t\t"+UserDictionary.Words.WORD +"\t"+UserDictionary.Words.FREQUENCY+"\n");
Log.d("Word",userDictString);
dictionaryTextView.setText(userDictString);
while (cursor.moveToNext())//zero if end of list in cursor(no more rows)
{
Log.d("Word",cursor.getString(columnWords));
int id = cursor.getInt(columnIndex);
int frequency = cursor.getInt(columnFrequency);
String word = cursor.getString(columnWords);
dictionaryTextView.append(("\n" + id + " - " + frequency + " - " + word));
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally {
cursor.close();
}
}
注意:我甚至清除了字典並重新啓動了手機,光標仍然顯示以前顯示的不存在的最後一個項目 –