tags:

views:

51

answers:

0

Hi guys. I'm trying to create a list activity that uses a SimpleCursorAdapter, but it just won't work. I know I'm getting the cursor correctly because when I have it print out to text all the values are there 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. My source code is below.

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(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();

}

}