使用SimplecursorAdapter时changeCursor崩溃

时间:2013-09-22 07:52:26

标签: android simplecursoradapter

我被困这个问题已经2天了。 我有一个名为 Mail_Content_SimpleCursor 的Custom SimpleCursorAdapter,在我的MainActivity中有以下代码:

private Mail_Content_SimpleCursor mail_content_cusor;
private Cursor mail_cursor;
 ListView Mail_contents;
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
...
Mail_contents = (ListView)findViewById(R.id.mail_list_view_item);
mail_cursor = mDB.getMailData("gmail@gmail.com"); // Cursor query read recods from database
        startManagingCursor(mail_cursor);
        String[] from = new String[]{"mail_From","mail_Subject","mail_Content"};
        int[] to = new int[] {R.id.mail_list_from,R.id.mail_list_subject,R.id.mail_list_content};
        mail_content_cusor = new Mail_Content_SimpleCursor(this, R.layout.main_mail_list_item, mail_cursor, from, to);
Mail_contents.setAdapter(mail_content_cusor);

}

然后,我想更改光标以使用函数

获取具有其他条件的其他记录
public void change(){
    mail_cursor = mDB.getMailData("yahoo@gmail.com"); // change where clause in query
    startManagingCursor(mail_cursor);
    mail_content_cusor.changeCursor(mail_cursor);
}

但是changeCursor函数使我的应用程序崩溃时出现此错误

09-22 14:07:18.189: E/AndroidRuntime(31931): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM mail_contents WHERE mail_user='gmail@gmail.com') 

1 个答案:

答案 0 :(得分:0)

请尝试使用swapCursor

根据文件:

SwapCursor:交换一个新的Cursor,返回旧的Cursor。与changeCursor(Cursor)不同,返回的旧Cursor不会关闭。