开发者

SimpleCursorAdapter crashes within an Activity

I'm trying to create an activity with a list that uses a SimpleCursorAdapter, but it just won't work. I'm creating the cursor correctly because when I make it print out text, all the values are printed and I know there's nothing wrong with the SimpleCursorAdapter class because when I used it with the image content provider, it displayed correctly. However, when I try to put the two together, my application crashes when I try to open the activity. Here is my source code:

package com.mao.crypt;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class OpenActivity extends ListActivity{

    Cursor cursor;

    @Override    
    public void onCreate(Bundle icicle){

        super.onCreate(icicle);
        SQLiteDatabase db = new NotesDBHelper(getApplicationContext()).getReadableDatabase();

        cursor = db.rawQuery("SELECT _id,title,text FROM notes ORDER BY title ASC", null);

        startManagingCursor开发者_如何学Go(cursor);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, new String[]{"title","text"},new int[]{android.R.id.text1, android.R.id.text2});
        setListAdapter(adapter);
    }

    public void onDestroy(){
        cursor.close();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id){
        cursor.moveToPosition(position);
        String title=cursor.getString(0);
        Intent data = new Intent();
        data.putExtra("title", title);   
        setResult(RESULT_OK,data);
            finish();
    }
}

Any suggestions on how to fix this?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜