Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Query 2 spinners

android sqlite spinners

This topic has been archived. This means that you cannot reply to this topic.
3 replies to this topic

#1 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 31 March 2015 - 09:34 AM

In my layout, i have 2 spiners. One for the name of the stations and the other for the facilities of the station. I want to put it in the way that if in the name spinner, i choose a certain name, then on the other spinner it should automatically change the facilities to the one for the particular row of the name selected.

This is my databaseHelper.  SQL.java

 

package com.azmal.example.com.facilitiesreviewapp;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
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;
    }

    // Method for fetching all the rows of the Establishment table ordered by the Establishment name
    public Cursor getFilteredRecords(String filter) {

        return db.query(DATABASE_TABLE, new String[] { "_id",KEY_STATION_ID, KEY_STATION_NAME, KEY_STATION_TYPE,KEY_FACILITIES_TYPE,KEY_LOCATION,
                        KEY_EMAIL}, "StationName like ?",
                new String[] { filter + "%" }, null, null, KEY_STATION_NAME);

    }


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

}

Here is the layout add_review.xml

 

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >


<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/AddReview"
            android:textSize="28sp" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="Station Name:" />

        <Spinner
            android:id="@+id/spinnerStationName"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="@string/ReviewDate" />

        <DatePicker
            android:id="@+id/DatePickerDOB"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="@string/FacilityType" />

        <Spinner
            android:id="@+id/spinnerFacilityType"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="@string/Rating" />


        <RatingBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/ratingBar" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15sp"
            android:text="@string/AdditionalComments" />

        <EditText
            android:layout_width="match_parent"
            android:layout_height="155dp"
            android:inputType="textMultiLine"
            android:ems="10"
            android:id="@+id/AdditionalComment" />

        <Button
            android:id="@+id/SaveReview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/SaveReview" />

    </LinearLayout>
   </ScrollView>



 </FrameLayout>
<ListView
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>

And here is the javafile.. AddReview.java.. it is empty because i didnt know where to begin with

 

package com.azmal.example.com.facilitiesreviewapp;

import android.content.res.TypedArray;
import android.os.Bundle;

public class AddReview extends BaseActivity {
    private String[] navMenuTitles;
    private TypedArray navMenuIcons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_review);
        navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items); // load
        // titles
        // from
        // strings.xml

        navMenuIcons = getResources()
                .obtainTypedArray(R.array.nav_drawer_icons);// load icons from
        // strings.xml

        set(navMenuTitles, navMenuIcons);
    }
}


#2 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 31 March 2015 - 05:54 PM

You should be more specific about how do you want us to help.

Still, one thing I can tell is I rather  PopupWindows with listviews, with fadeaway effect and semi-transparent backgrounds to spinners.

 

I've recently had the very same trouble and I went PopupWindow, and I loved it :)



#3 DarkHeart

DarkHeart

    CC Newcomer

  • Member
  • PipPip
  • 16 posts

Posted 01 April 2015 - 12:58 AM

Thanks for the advice. i will definitely look into the popupwindows thing on google. My question was, on the table 1, I have a row for Station names, and another row for facilities of the station. Now say when entering data i put Grand Central as the Station name and the facility i put for it is Wifi. And then i put another station name, say, King Street Station and then i put the facility for it as Lifts. Now what i want is if i enter Grand central in the top spinner (or as u prefer Popupwindows with listviews) then the spinner/list down there should automatical show wifi. And if on the name i chose King street then on the facility spinner down there is should automatically change to Lifts.

You should be more specific about how do you want us to help.

Still, one thing I can tell is I rather  PopupWindows with listviews, with fadeaway effect and semi-transparent backgrounds to spinners.

 

I've recently had the very same trouble and I went PopupWindow, and I loved it :)



#4 BlackRabbit

BlackRabbit

    CodeCall Legend

  • Expert Member
  • PipPipPipPipPipPipPipPip
  • 3871 posts

Posted 01 April 2015 - 06:07 PM

In that case. What you need is to trigger the action. You do that adding a TextChange event to the edit text.

 

About the popupwindow, this is a great tutorial






Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download