0

I have created a database with a cursor. I want to show it in a list view. This is my Activity file:

package com.ucas.course;

import java.util.List;

import org.w3c.dom.Comment;

import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class SQLView extends ListActivity {

 public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_example);
        UCAS info = new UCAS(this);
        info.open();
        String values = info.getData();
        info.close();
        AString[] columns = new String[] {UCAS.KEY_UNIVERSITY};
        int[] to = new int[] { R.id.name_entry, R.id.number_entry };
        startManagingCursor(c);
        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.sqlview, c, columns, to);
        this.setListAdapter(mAdapter);

 }



}

AT the moment there are no error apart from one cursor error "invalid statement in fillWindow()", I don't know if that has something to do with my issue but when I start the activity I just get a blank screen

public Cursor getData() {
    // TODO Auto-generated method stub
    String[] columns = new String[]{KEY_ROWID, KEY_UNIVERSITY, KEY_OFFER};
    Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);


    return c;
}
2
  • number of itmes in "columns" and "to" must be equal! Also cursor should contain "_id" column (I assume that this is your KEY_ROWID column). Commented Jul 17, 2012 at 10:11
  • I've changed them to this String[] columns = new String[] {UCAS.KEY_ROWID,UCAS.KEY_UNIVERSITY, UCAS.KEY_OFFER}; int[] to = new int[] { R.id.KEY_ROWID, R.id.KEY_UNIVERSITY, R.id.KEY_OFFER}; I assume that's what you meant but I still get the error and nothing shows Commented Jul 17, 2012 at 10:26

2 Answers 2

1

Lists can and in your case should use cursors to back their data. There's no reason for you to build a result string to hold your query values. You can pass the cursor directly to the adapter.

public String getData() {
     String[] columns = new String[]{KEY_ROWID, KEY_UNIVERSITY, 
         KEY_COURSE, KEY_UCAS, KEY_SATISFACTION, KEY_EMPLOYED, 
         KEY_OFFER, KEY_OTHER};

     Cursor c = ourDatabase.query(DATABASE_TABLE, 
         columns, null, null, null, null, null);

     return c;

}

Once you have the cursor, bind it to the ListActivity using a SimpleCursorAdapter.

Sign up to request clarification or add additional context in comments.

1 Comment

hi thanks that was usefull but now i have hit another wall i've edited the post above with more infomation and my new script
0

there is small change to your function

    public String getData() {
     String[] columns = new String[]{KEY_ROWID, KEY_UNIVERSITY, 
         KEY_COURSE, KEY_UCAS, KEY_SATISFACTION, KEY_EMPLOYED, 
         KEY_OFFER, KEY_OTHER};

     Cursor c = mDBHelper.getReadableDatabase.query(DATABASE_TABLE, 
         columns, null, null, null, null, null);

     return c;

}

4 Comments

in onDestroy method of your activity close the cursor
how do I close the cursor? sorry i'm new to this stuff
((CursorAdapter) this.getListAdapter()).getCursor().close();
thats not changed anything sorry. could have somthing to do with my XML's

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.