Search code examples
androidandroid-sdk-tools

getText() from listview created by database


I have a list populated by a Database and I am running the OnItemClick() method. What would I call to get the text from the item that was clicked?

public void DisplayRecord(Cursor c) {
    Cursor c1 = DBAdapter.getAllRecords();
    startManagingCursor(c1);

    String[] from = new String[] { DBAdapter.KEY_ITEM };
    int[] to = new int[] { R.id.text1 };

    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.row,
            c1, from, to);
    setListAdapter(notes);
}

@Override
public void onListItemClick(ListView l, View view, int position, long id) {
    switch (position) {
    case 0:
        //method to getText()?

        break;
    }
}

LogCat:

02-03 22:34:11.174: E/AndroidRuntime(1561): FATAL EXCEPTION: main
02-03 22:34:11.174: E/AndroidRuntime(1561): java.lang.IllegalStateException: database not open
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1312)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1271)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1391)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at com.Logik.dawn.of.ages.DBAdapter.getAllRecords(DBAdapter.java:87)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at com.Logik.dawn.of.ages.Inventory.onListItemClick(Inventory.java:130)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.app.ListActivity$2.onItemClick(ListActivity.java:342)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.widget.ListView.performItemClick(ListView.java:3561)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1831)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.os.Handler.handleCallback(Handler.java:587)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.os.Looper.loop(Looper.java:143)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at android.app.ActivityThread.main(ActivityThread.java:4293)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at java.lang.reflect.Method.invokeNative(Native Method)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at java.lang.reflect.Method.invoke(Method.java:507)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-03 22:34:11.174: E/AndroidRuntime(1561):     at dalvik.system.NativeStart.main(Native Method)

Solution

  • Cursor c = this.getContentResolver().query(DBAdapter.getAllRecords(),null,null,null,null);
    
    @Override
    public void onListItemClick(ListView l, View view, int position, long id) {                
          c.moveToPosition(position);
          String s = c.getString(c.getColumnIndex(DBAdapter.KEY_ITEM));
    
    }