0
公共類MainActivity擴展AppCompatActivity {從Android的用戶詞典訪問數據時光標出錯?
// For the SimpleCursorAdapter to match the UserDictionary columns to layout items.
private static final String[] COLUMNS_TO_BE_BOUND = new String[]{
UserDictionary.Words.WORD,
UserDictionary.Words.FREQUENCY
};
private static final int[] LAYOUT_ITEMS_TO_FILL = new int[]{
android.R.id.text1,
android.R.id.text2
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView which will be populated with the Dictionary ContentProvider data.
ListView dictListView = (ListView) findViewById(R.id.dictionary_list_view);
//TextView dictTextView = (TextView) findViewById(R.id.dictionary_text_view);
Cursor cursor = null;
try {
// Get the ContentResolver which will send a message to the ContentProvider.
ContentResolver resolver = getContentResolver();
// Get a Cursor containing all of the rows in the Words table.
cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.two_line_list_item,
cursor,
COLUMNS_TO_BE_BOUND,
LAYOUT_ITEMS_TO_FILL,
0
);
dictListView.setAdapter(adapter);
} catch (Exception e) {
e.getMessage();
} finally {
cursor.close();
}
}
}
/* 如果我不關閉cusor然後會出現內存泄漏。但編譯器沒有顯示它! 我想知道這裏究竟發生了什麼? 在此先感謝! */
您不必在適配器中關閉遊標,直到完成使用它們 – tyczj