简体   繁体   中英

SQLite database list view

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;
}

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.

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;

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM