2015-04-03 57 views
-2

我使用了一個Array複選框來將數據保存到表中的一個列。我想在微調器中顯示該數組中的每個項目,以便可以進行選擇。將數組從數據庫顯示到微調器中

我想從名爲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; 

    } 

} 
+0

只需使用[簡單] CursorAdapter的,順便說一句,爲什麼你叫moveToFirst返回光標之前? – pskink 2015-04-03 14:08:22

+0

pskink。這些方法是在其他部分執行的,而不是我的問題所針對的部分。你可以給我一個提示或至少一個數據庫的代碼? – D4rkH34rt 2015-04-03 14:12:53

+0

谷歌爲:android微調遊標適配器的例子 – pskink 2015-04-03 14:17:09

回答

0

我試過這個,它工作。但唯一的問題是,文本數組全部進入一行,而不是彼此獨立。其表現就像無線網絡連接,坡道,電梯..而不是一直在自己的行每個單詞

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

 
    }

相關問題