将数据库从数据库显示到微调器中

时间:2015-04-03 13:47:26

标签: android sqlite arraylist spinner

我使用了一个充满复选框的数组来将数据保存到表的一个列中。我希望在微调器中显示此数组中的每个项目,以便可以选择。

我想从名为KEY_FACILITIES_TYPE的列中获取内容,并将数组中保存的所有项目显示到微调器中。那么我可以在数据库中放入什么方法?以及如何在微调器中显示它?这是我的数据库文件

package com.example.com.facilitiesreviewapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// creating the class
public class SQL {

    /////////////////////////////////////////////////////////////////////
    //  Constants & Data
    /////////////////////////////////////////////////////////////////////
    // For logging:
    private static final String TAG = "DBAdapter";

    // declaring and initializing data Primary Key Fields for both tables
    public static final String KEY_ROWID = "_id";
    public static final int COL_ROWID = 0;

    public static final String KEY_REVIEWROWID = "_id";
    public static final int COL_REVROWID = 0;

    //declaring and initializing other database Fields for both tables
    public static final String KEY_STATION_NAME = "StationName";
    public static final String KEY_STATION_TYPE = "StationType";
    public static final String KEY_FACILITIES_TYPE = "Facilities";
    public static final String KEY_LOCATION = "Location";
    public static final String KEY_EMAIL = "Email";
    public static final String KEY_STATION_ID = "userId";

    public static final String KEY_REV_STATION_NAME = "revStationName";
    public static final String KEY_DATE = "Date";
    public static final String KEY_REV_FACILITY = "revFacility";
    public static final String KEY_RATING = "Rating";
    public static final String KEY_COMMENT = "Comment";


    //Defining Field numbers for both tables
    public static final int COL_STATION_NAME = 2;
    public static final int COL_STATION_TYPE = 3;
    public static final int COL_FACILITIES_TYPE = 4;
    public static final int COL_LOCATION = 5;
    public static final int COL_EMAIL = 5;
    public static final int COL_STATION_ID = 1;

    public static final int COL_REV_STATION_NAME = 1;
    public static final int COL_DATE = 2;
    public static final int COL_REV_FACILITY = 3;
    public static final int COL_RATING = 4;
    public static final int COL_COMMENTS = 5;

    // declaring and initializing a string to get all fields from Establishment Table
    public static final String[] ALL_KEYS = new String[]{KEY_ROWID, KEY_STATION_NAME,
            KEY_STATION_TYPE, KEY_FACILITIES_TYPE, KEY_LOCATION, KEY_EMAIL, KEY_STATION_ID};

    //declaring and initializing a string to get all fields from Review Table
    public static final String[] ALL_REV_KEYS = new String[]{KEY_REVIEWROWID, KEY_REV_STATION_NAME,
            KEY_DATE, KEY_REV_FACILITY, KEY_RATING, KEY_COMMENT};


    // declaring and initializing database name, and the two tables
    public static final String DATABASE_NAME = "FacilitiesReview";
    public static final String DATABASE_TABLE = "Stations";
    public static final String DATABASE_TABLE2 = "Review";

    // declaring and initializing a variable for tracking DB version if a new version of
    // the application changes the format.
    public static final int DATABASE_VERSION = 2;

    // declaring and initializing a string to create the Establishment Table
    private static final String DATABASE_CREATE_SQL =
            "create table " + DATABASE_TABLE
                    + " (" + KEY_ROWID + " integer primary key autoincrement, "
                    + KEY_STATION_NAME + " text,"
                    + KEY_STATION_TYPE + " text,"
                    + KEY_FACILITIES_TYPE + " text,"
                    + KEY_LOCATION + " text,"
                    + KEY_EMAIL + " text,"
                    + KEY_STATION_ID + " text"
                    + ");";

    // declaring and initializing a string to create the Review Table
    private static final String DATABASE2_CREATE_SQL =
            "create table " + DATABASE_TABLE2
                    + " (" + KEY_REVIEWROWID + " integer primary key autoincrement, "
                    + KEY_REV_STATION_NAME + " text,"
                    + KEY_DATE + " text,"
                    + KEY_REV_FACILITY + " text,"
                    + KEY_RATING + " text,"
                    + KEY_COMMENT + " text"
                    + ");";

    // Creating the Context of application who uses us.
    private final Context context;

    private DatabaseHelper myDBHelper;
    private SQLiteDatabase db;

    /////////////////////////////////////////////////////////////////////
    //  Public methods:
    /////////////////////////////////////////////////////////////////////

    public SQL(Context ctx) {
        this.context = ctx;
        myDBHelper = new DatabaseHelper(context);
    }

    // Opening the database connection.
    public SQL open() {
        db = myDBHelper.getWritableDatabase();
        return this;
    }

    // Close the database connection.
    public void close() {
        myDBHelper.close();
    }

    // Method for Adding a new establishment to the database.
    public long insertStation(String strStationID, String strName, String strStationType, String strFacilities, String strLocation,
                              String strEmail) {

        //assigning user input with database rows
        ContentValues values = new ContentValues();
        values.put(KEY_STATION_ID, strStationID);
        values.put(KEY_STATION_NAME, strName);
        values.put(KEY_STATION_TYPE, strStationType);
        values.put(KEY_FACILITIES_TYPE, strFacilities);
        values.put(KEY_LOCATION, strLocation);
        values.put(KEY_EMAIL, strEmail);


        // Inserting it into the database
        return db.insert(DATABASE_TABLE, null, values);
    }

    // Method for Adding a new review to the database.
    public long insertReviewRow(String strRevStation, String strDate, String strRevFacility, Float strRating, String strComment) {

        //assigning user input with database rows
        ContentValues review_values = new ContentValues();
        review_values.put(KEY_REV_STATION_NAME, strRevStation);
        review_values.put(KEY_DATE, strDate);
        review_values.put(KEY_REV_FACILITY, strRevFacility);
        review_values.put(KEY_RATING, strRating);
        review_values.put(KEY_COMMENT, strComment);

        //Inserting it into the database
        return db.insert(DATABASE_TABLE2, null, review_values);
    }

    /* method for Deleting a row from the database Establishment Table,
    by rowId (primary key/ Establishment)
    from the Establishment Table */
    public boolean deleteRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        return db.delete(DATABASE_TABLE, where, null) != 0;
    }


    // Method for fetching all data in the database from the Establishment Table.
    public Cursor getAllRows() {
        String where = null;
        Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                where, null, null, null, KEY_STATION_NAME, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Method for fetching all data in the database from the Review Table of the particular establishment
    // (by using passed establishment)
    public Cursor getAllReviewRows(String establishment) {
        //String where = null;
        String where = KEY_REV_STATION_NAME + " = '" + establishment + "'";
        Cursor c = db.query(true, DATABASE_TABLE2, ALL_REV_KEYS,
                where, null, null, null, KEY_DATE, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

    // Method for fetching data for a particular establishment (by using passed rowId)
    public Cursor getRow(long rowId) {
        String where = KEY_ROWID + "=" + rowId;
        Cursor c = db.query(true, DATABASE_TABLE, ALL_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }


    /////////////////////////////////////////////////////////////////////
    //  Private Helper Classes:
    /////////////////////////////////////////////////////////////////////

    /**
     * Private class which handles database creation and upgrading.
     * Used to handle low-level database access.
     */
    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        // Method for creating the Database
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_CREATE_SQL);
            _db.execSQL(DATABASE2_CREATE_SQL);
        }

        @Override
        // Method for upgrading the database
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading application's database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data!");

            // Destroying old database:
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE2);
            // Recreate new database:
            onCreate(_db);
        }
    }


    public Cursor fetchStationsByName(String inputText) throws SQLException {
        Log.w(TAG, inputText);
        Cursor mCursor = null;
        if (inputText == null || inputText.length() == 0) {
            mCursor = db.query(DATABASE_TABLE, new String[]{KEY_ROWID,
                            KEY_STATION_ID, KEY_STATION_NAME, KEY_STATION_TYPE, KEY_FACILITIES_TYPE,KEY_LOCATION,KEY_EMAIL},
                    null, null, null, null, null);

        } else {
            mCursor = db.query(true, DATABASE_TABLE, new String[]{KEY_ROWID,
                            KEY_STATION_ID, KEY_STATION_NAME, KEY_STATION_TYPE, KEY_FACILITIES_TYPE,KEY_LOCATION,KEY_EMAIL},
                    KEY_STATION_NAME + " like '%" + inputText + "%'", null,
                    null, null, null, null);
        }
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

}

1 个答案:

答案 0 :(得分:0)

我尝试了这个并且它有效。但唯一的问题是,文本数组都是一行而不是彼此独立。它显示像wifi,斜坡,升降机..而不是每个单词都在自己的行



public void loadtospinner() {

        Spinner spFacilityType = (Spinner) findViewById(R.id.spinnerFacilityType);

        Cursor c = dbHelper.getAllRows();
        ArrayList<String> al = new ArrayList<String>();

        c.moveToFirst();
        while (!c.isAfterLast()) {

            String name = c.getString(c.getColumnIndex(dbHelper.KEY_FACILITIES_TYPE));
            al.add(name);
            c.moveToNext();
        }

        ArrayAdapter<String> aa1 = new ArrayAdapter<String>(
                getApplicationContext(), R.layout.spinner_item, R.id.textView1,
                al);

        spFacilityType.setAdapter(aa1);

        // closing database
        dbHelper.close();

    }
&#13;
&#13;
&#13;