Android Studio:根据上一个微调器上的用户选择填充微调器

时间:2018-11-07 08:40:22

标签: android android-spinner

这是我第一次问有关stackoverflow的问题。首先,我想说谢谢您花时间阅读我的问题,如果可以的话,请提出解决方案。

我试图根据用户在 Store 微调器中选择的内容来填充我的 Aisle 微调器。以下是一些图片,可以帮助您更好地了解我要传达的内容。 Spinners.1 Spinners.2

在Spinners.1中,两个按钮正上方的两个旋转器是我要填充的按钮。在Spinners.2中,您将看到第一个微调器的用户选项(不同的商店),根据用户的选择,将为第二个微调器中的用户提供特定商店中过道的列表。 (我还试图找出如何在第一个微调器中将商店地址向下移动一行,以便您可以更好地查看商店名称和地址。)我还附加了商店的图像和从数据库中选择的过道表。 Store and Aisle Tables

我还将包括我的创建和插入查询。 (使用Azure SQL数据库)

创建查询

CREATE TABLE list(
    user_email  VARCHAR(50)     not null,
    list_id     BIGINT          not null    IDENTITY(1,1),
    list_name   VARCHAR(50)     not null,

    CONSTRAINT list_id_pk PRIMARY KEY (list_id),
    CONSTRAINT user_email_fk_list FOREIGN KEY (user_email) REFERENCES user_information(user_email)
);

DROP TABLE list;
ALTER TABLE list DROP CONSTRAINT list_id_pk;
ALTER TABLE list DROP CONSTRAINT user_email_fk_list;

CREATE TABLE item_type(
    item_type   VARCHAR(50)     not null,

    CONSTRAINT item_type_pk PRIMARY KEY (item_type)
);

DROP TABLE item_type;
ALTER TABLE item_type DROP CONSTRAINT item_type_pk;

CREATE TABLE measurement(
    measurement_type    VARCHAR(50)     not null,

    CONSTRAINT measurement_type_pk PRIMARY KEY (measurement_type)
);

DROP TABLE measurement;
ALTER TABLE measurement DROP CONSTRAINT measurement_type_pk;

CREATE TABLE store(
    store_id            BIGINT          not null    IDENTITY(1,1),
    store_name          VARCHAR(50)     not null,
    store_street        VARCHAR(50)     not null,
    store_city          VARCHAR(50)     not null,
    store_state         VARCHAR(50)     not null,
    store_zipcode       INTEGER         not null,
    store_country       VARCHAR(50)     not null,
    store_phone_number  VARCHAR(20)     not null,

    CONSTRAINT store_id_pk PRIMARY KEY (store_id)
);

DROP TABLE store;
ALTER TABLE store DROP CONSTRAINT store_id_pk;

CREATE TABLE item_location(
    store_id            BIGINT          not null,
    aisle               VARCHAR(50)     not null,

    CONSTRAINT aisle_and_store_id_pk PRIMARY KEY (aisle,store_id)
);

DROP TABLE item_location;
ALTER TABLE item_location DROP CONSTRAINT aisle_and_store_id_pk;

CREATE TABLE items_in_list(
    list_id             BIGINT          not null,
    item_id             BIGINT          not null    IDENTITY(1,1),
    item_name           VARCHAR(50)     not null,
    item_type           VARCHAR(50)     not null,
    item_price          DECIMAL(10,2)   null,
    measurement_type    VARCHAR(50)     null,
    item_quantity       DECIMAL(10,2)   null,
    store_id            BIGINT          null,
    aisle               VARCHAR(50)     null,

    CONSTRAINT item_id_pk PRIMARY KEY (item_id),
    CONSTRAINT list_id_fk_items_in_list FOREIGN KEY (list_id) REFERENCES list(list_id),
    CONSTRAINT item_type_fk_items_in_list FOREIGN KEY (item_type) REFERENCES item_type(item_type),
    CONSTRAINT measurement_type_fk_items_in_list FOREIGN KEY (measurement_type) REFERENCES measurement(measurement_type),
    CONSTRAINT aisle_and_store_id_fk_items_in_list FOREIGN KEY (aisle,store_id) REFERENCES item_location(aisle,store_id)
);

DROP TABLE items_in_list;
ALTER TABLE items_in_list DROP CONSTRAINT item_id_pk;
ALTER TABLE items_in_list DROP CONSTRAINT list_id_fk_items_in_list;
ALTER TABLE items_in_list DROP CONSTRAINT item_type_fk_items_in_list;
ALTER TABLE items_in_list DROP CONSTRAINT measurement_type_fk_items_in_list;
ALTER TABLE items_in_list DROP CONSTRAINT aisle_and_store_id_fk_items_in_list;

SELECT * FROM user_information;
SELECT * FROM list;
SELECT * FROM item_type;
SELECT * FROM measurement;
SELECT * FROM store;
SELECT * FROM item_location;
SELECT * FROM items_in_list;

插入查询

SELECT * FROM user_information;

SELECT * FROM list;
INSERT INTO list VALUES('testemail1@test.com', 'Birthday Party');
INSERT INTO list VALUES('testemail2@test.com', 'Grocery');
INSERT INTO list VALUES('testemail3@test.com', 'Graduation Party');
INSERT INTO list VALUES('testemail4@test.com', 'Anniversary Party');
INSERT INTO list VALUES('testemail5@test.com', 'Girls Night');
INSERT INTO list VALUES('testemail6@test.com', 'Sleepover');
INSERT INTO list VALUES('testemail7@test.com', 'Birthday Party');
INSERT INTO list VALUES('testemail8@test.com', 'Dance');
INSERT INTO list VALUES('testemail9@test.com', 'Grocery');
INSERT INTO list VALUES('testemail10@test.com', 'Grocery');
INSERT INTO list VALUES('testemail1@test.com', 'Grocery');
INSERT INTO list VALUES('testemail1@test.com', 'Graduation Party');
INSERT INTO list VALUES('testemail1@test.com', 'Anniversary Party');
INSERT INTO list VALUES('testemail1@test.com', 'Dance');

SELECT * FROM item_type;
INSERT INTO item_type VALUES('Fruits');
INSERT INTO item_type VALUES('Vegetables');
INSERT INTO item_type VALUES('Beverages');
INSERT INTO item_type VALUES('Dairy');
INSERT INTO item_type VALUES('Meat');
INSERT INTO item_type VALUES('Pasta/Rice');
INSERT INTO item_type VALUES('Seafood');
INSERT INTO item_type VALUES('Spices/Sauces/Condiments');
INSERT INTO item_type VALUES('Snacks');
INSERT INTO item_type VALUES('Bakery');
INSERT INTO item_type VALUES('Baking');
INSERT INTO item_type VALUES('Produce');
INSERT INTO item_type VALUES('Deli');
INSERT INTO item_type VALUES('Frozen Foods');
INSERT INTO item_type VALUES('Sweets');
INSERT INTO item_type VALUES('Alcohol');

SELECT * FROM measurement;
INSERT INTO measurement VALUES('Milligram (mg)');
INSERT INTO measurement VALUES('Gram (g)');
INSERT INTO measurement VALUES('Kilogram (kg)');
INSERT INTO measurement VALUES('Ounce (oz.)');
INSERT INTO measurement VALUES('Pound (lb.)');
INSERT INTO measurement VALUES('Millilitre (ml)');
INSERT INTO measurement VALUES('Litre (L)');
INSERT INTO measurement VALUES('Fluid ounce (fl.oz.)');
INSERT INTO measurement VALUES('Pint (pt.)');
INSERT INTO measurement VALUES('Quart (qt.)');
INSERT INTO measurement VALUES('Gallon (gal.)');
INSERT INTO measurement VALUES('Piece (pc)');
INSERT INTO measurement VALUES('Pair (pa)');
INSERT INTO measurement VALUES('Dozen (dz)');
INSERT INTO measurement VALUES('Millimetre (mm)');
INSERT INTO measurement VALUES('Centimetre (cm)');
INSERT INTO measurement VALUES('Metre (m)');
INSERT INTO measurement VALUES('Inch (in.)');
INSERT INTO measurement VALUES('Foot (ft.)');
INSERT INTO measurement VALUES('Yard (yd)');

SELECT * FROM store;
INSERT INTO store VALUES('Target', '531 Brookwood Village', 'Homewood', 'AL', '35209', 'USA', '(205)994-7531');
INSERT INTO store VALUES('Walmart', '209 Lakeshore Parkway', 'Homewood', 'AL', '35209', 'USA', '(205)945-8692');
INSERT INTO store VALUES('Sam''s Club', '201 Lakeshore Parkway', 'Homewood', 'AL', '35209', 'USA', '(205)941-3326');
INSERT INTO store VALUES('Publix', '784 Montgomery Highway', 'Vestavia Hills', 'AL', '35216', 'USA', '(205)824-6005');
INSERT INTO store VALUES('The Fresh Market', '549 Brookwood Village', 'Homewood', 'AL', '35209', 'USA', '(205)414-0017');

SELECT * FROM item_location;
INSERT INTO item_location VALUES('1', 'G22'); --ketchup
INSERT INTO item_location VALUES('1', 'G15'); --bell peppers
INSERT INTO item_location VALUES('1', 'G35'); --goldfish
INSERT INTO item_location VALUES('1', 'G8'); --strawberries
INSERT INTO item_location VALUES('1', 'G46'); --milk
INSERT INTO item_location VALUES('1', 'G23'); --spaghetti

SELECT * FROM items_in_list;
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','apple','Fruits');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','banana','Fruits');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','orange','Fruits');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','milk','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','cheddar cheese','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','mozzarella cheese','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','strawberry yogurt','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('2','butter','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('3','milk','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('3','butter','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('3','flour','Baking');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('3','sugar','Baking');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('9', 'strawberries', 'Fruits', '3');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('9', 'grapes', 'Fruits', '3');
INSERT INTO items_in_list VALUES('9', 'Barilla Spaghetti Pasta 16oz', 'Pasta/Rice', '1.39', 'Pound (lb.)', '4', '1', 'G23');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','apple','Fruits', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','banana','Fruits', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','orange','Fruits', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','Market Pantry milk','Dairy', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','cheddar cheese','Dairy', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','mozzarella cheese','Dairy', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','strawberry yogurt','Dairy', '4');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('11','butter','Dairy', '4');
INSERT INTO items_in_list VALUES('12','Market Pantry milk','Dairy', '4.49', 'Gallon (gal.)', '1', '1', 'G46');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('12','butter','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('12','flour','Baking');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('12','sugar','Baking');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('13','butter','Dairy');
INSERT INTO items_in_list VALUES('13','Market Pantry milk','Dairy', '4.49', 'Gallon (gal.)', '1', '1', 'G46');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('13','butter','Dairy');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('13','flour','Baking');
INSERT INTO items_in_list (list_id, item_name, item_type) VALUES('13','sugar','Baking');
INSERT INTO items_in_list VALUES('14','strawberries','Fruits', '3.59', 'Quart (qt.)', '2', '1', 'G8');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('14','grapes','Fruits', '2');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('14','butter','Dairy', '2');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('14','flour','Baking', '2');
INSERT INTO items_in_list (list_id, item_name, item_type, store_id) VALUES('14','sugar','Baking', '2');

这是我的用于填充微调器的Android Studio代码:

try {
            conn = connectionClass();

            if(conn == null){
                return;
            }else{
                Statement stmt = conn.createStatement();

                String measurementQuery = "SELECT measurement_type FROM measurement";
                ResultSet measurementRS = stmt.executeQuery(measurementQuery);
                String measurement = "";
                ArrayList<String> measurements = new ArrayList<String>();
                while(measurementRS.next()){
                    measurement = measurementRS.getString("measurement_type");
                    measurements.add(measurement);
                }
                ArrayAdapter measurementAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, measurements);
                measurementAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerMeasurement.setAdapter(measurementAdapter);

                String itemTypeQuery = "SELECT item_type FROM item_type";
                ResultSet itemTypeRS = stmt.executeQuery(itemTypeQuery);
                String itemType = "";
                ArrayList<String> itemTypes = new ArrayList<String>();
                while(itemTypeRS.next()){
                    itemType = itemTypeRS.getString("item_type");
                    itemTypes.add(itemType);
                }
                ArrayAdapter itemTypeAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, itemTypes);
                itemTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerItemType.setAdapter(itemTypeAdapter);


                String storeQuery = "SELECT store_id, store_name, store_street, store_city, store_state, store_zipcode FROM store";
                ResultSet storeRS = stmt.executeQuery(storeQuery);
                String store, storeName, storeStreet, storeCity, storeState, storeZipcode = "";
                ArrayList<String> stores = new ArrayList<String>();
                while(storeRS.next()){
                    storeName = storeRS.getString("store_name");
                    storeStreet = storeRS.getString("store_street");
                    storeCity = storeRS.getString("store_City");
                    storeState = storeRS.getString("store_State");
                    storeZipcode = storeRS.getString("store_zipcode");
                    store = storeName + "\n" + storeStreet + "\n" + storeCity + ", " + storeState + " " + storeZipcode;
                    stores.add(store);
                }
                ArrayAdapter storeAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, stores);
                storeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                spinnerStore.setAdapter(storeAdapter);
                /*spinnerStore.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                        String store = parent.getItemAtPosition(position).toString();
                        String aisleQuery = "SELECT aisle FROM item_location WHERE store_id = '1'";
                        ResultSet aisleRS = stmt.executeQuery(aisleQuery);
                        String aisle = "";
                        ArrayList<String> aisles = new ArrayList<String>();
                        while(aisleRS.next()){
                             aisle = aisleRS.getString("measurement_type");
                             aisles.add(aisle);
                        }
                        ArrayAdapter aisleAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, aisles);
                        aisleAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                        spinnerMeasurement.setAdapter(aisleAdapter);
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> parent) {
                    }
                });*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

希望能对您有所帮助

1。)在第一个微调器中正确设置我的商店名称和地址

商店名称

街道

城市,州邮政编码

2。)能够根据第一个微调器中的用户选择来填充我的第二个微调器。

希望我能给您足够的信息。再次感谢您的时间和帮助!

0 个答案:

没有答案