Как я могу реализовать динамический вариант в приложении электронной коммерции? - PullRequest
0 голосов
/ 23 октября 2019

В опциях у меня есть 4 типа [выпадающий, multi_dropdown, radio, checkbox], теперь я должен динамически реализовать в моем приложении и после выбора получить общую цену и вставить в локальную базу данных со всеми выбранными опциями.

Ответ API

{
"id": 25,
    "tax_class_id": null,
    "slug": "asxasd",
    "price": {
        "amount": "23.0000",
        "formatted": "$23.00",
        "currency": "USD"
    },
"options": [
        {
            "id": 18,
            "type": "dropdown",
            "is_required": false,
            "is_global": false,
            "position": 0,
            "deleted_at": null,
            "created_at": "2019-10-22 04:07:44",
            "updated_at": "2019-10-22 04:07:44",
            "name": "Size",
            "pivot": {
                "product_id": 25,
                "option_id": 18
            },
            "translations": [
                {
                    "id": 18,
                    "option_id": 18,
                    "locale": "en",
                    "name": "Size"
                }
            ],
            "values": [
                {
                    "id": 66,
                    "option_id": 18,
                    "price": {
                        "amount": "12.0000",
                        "formatted": "$12.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 04:07:44",
                    "updated_at": "2019-10-22 04:07:44",
                    "label": "L",
                    "translations": [
                        {
                            "id": 66,
                            "option_value_id": 66,
                            "locale": "en",
                            "label": "L"
                        }
                    ]
                },
                {
                    "id": 67,
                    "option_id": 18,
                    "price": {
                        "amount": "10.0000",
                        "formatted": "$10.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 04:07:44",
                    "updated_at": "2019-10-22 04:07:44",
                    "label": "S",
                    "translations": [
                        {
                            "id": 67,
                            "option_value_id": 67,
                            "locale": "en",
                            "label": "S"
                        }
                    ]
                }
            ]
        },
        {
            "id": 20,
            "type": "checkbox",
            "is_required": false,
            "is_global": false,
            "position": 1,
            "deleted_at": null,
            "created_at": "2019-10-22 04:22:42",
            "updated_at": "2019-10-22 04:22:42",
            "name": "ASD",
            "pivot": {
                "product_id": 25,
                "option_id": 20
            },
            "translations": [
                {
                    "id": 20,
                    "option_id": 20,
                    "locale": "en",
                    "name": "ASD"
                }
            ],
            "values": [
                {
                    "id": 71,
                    "option_id": 20,
                    "price": {
                        "amount": "1.0000",
                        "formatted": "$1.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 04:22:42",
                    "updated_at": "2019-10-22 04:22:42",
                    "label": "A",
                    "translations": [
                        {
                            "id": 71,
                            "option_value_id": 71,
                            "locale": "en",
                            "label": "A"
                        }
                    ]
                },
                {
                    "id": 72,
                    "option_id": 20,
                    "price": {
                        "amount": "2.0000",
                        "formatted": "$2.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 04:22:42",
                    "updated_at": "2019-10-22 04:22:42",
                    "label": "B",
                    "translations": [
                        {
                            "id": 72,
                            "option_value_id": 72,
                            "locale": "en",
                            "label": "B"
                        }
                    ]
                }
            ]
        },
        {
            "id": 21,
            "type": "radio",
            "is_required": false,
            "is_global": false,
            "position": 2,
            "deleted_at": null,
            "created_at": "2019-10-22 04:23:16",
            "updated_at": "2019-10-22 04:23:16",
            "name": "AQW",
            "pivot": {
                "product_id": 25,
                "option_id": 21
            },
            "translations": [
                {
                    "id": 21,
                    "option_id": 21,
                    "locale": "en",
                    "name": "AQW"
                }
            ],
            "values": [
                {
                    "id": 73,
                    "option_id": 21,
                    "price": {
                        "amount": "2.0000",
                        "formatted": "$2.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 04:23:16",
                    "updated_at": "2019-10-22 04:23:16",
                    "label": "w",
                    "translations": [
                        {
                            "id": 73,
                            "option_value_id": 73,
                            "locale": "en",
                            "label": "w"
                        }
                    ]
                },
                {
                    "id": 74,
                    "option_id": 21,
                    "price": {
                        "amount": "5.0000",
                        "formatted": "$5.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 04:23:16",
                    "updated_at": "2019-10-22 04:23:16",
                    "label": "r",
                    "translations": [
                        {
                            "id": 74,
                            "option_value_id": 74,
                            "locale": "en",
                            "label": "r"
                        }
                    ]
                }
            ]
        },
        {
            "id": 22,
            "type": "multiple_select",
            "is_required": false,
            "is_global": false,
            "position": 3,
            "deleted_at": null,
            "created_at": "2019-10-22 04:23:52",
            "updated_at": "2019-10-22 04:23:52",
            "name": "qwert",
            "pivot": {
                "product_id": 25,
                "option_id": 22
            },
            "translations": [
                {
                    "id": 22,
                    "option_id": 22,
                    "locale": "en",
                    "name": "qwert"
                }
            ],
            "values": [
                {
                    "id": 75,
                    "option_id": 22,
                    "price": {
                        "amount": "33.0000",
                        "formatted": "$33.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 04:23:52",
                    "updated_at": "2019-10-22 04:23:52",
                    "label": "ws",
                    "translations": [
                        {
                            "id": 75,
                            "option_value_id": 75,
                            "locale": "en",
                            "label": "ws"
                        }
                    ]
                },
                {
                    "id": 76,
                    "option_id": 22,
                    "price": {
                        "amount": "3.0000",
                        "formatted": "$3.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 04:23:52",
                    "updated_at": "2019-10-22 04:23:52",
                    "label": "df",
                    "translations": [
                        {
                            "id": 76,
                            "option_value_id": 76,
                            "locale": "en",
                            "label": "df"
                        }
                    ]
                },
                {
                    "id": 77,
                    "option_id": 22,
                    "price": {
                        "amount": "34.0000",
                        "formatted": "$34.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 2,
                    "created_at": "2019-10-22 04:23:52",
                    "updated_at": "2019-10-22 04:23:52",
                    "label": "dfgh",
                    "translations": [
                        {
                            "id": 77,
                            "option_value_id": 77,
                            "locale": "en",
                            "label": "dfgh"
                        }
                    ]
                }
            ]
        },
        {
            "id": 23,
            "type": "checkbox",
            "is_required": false,
            "is_global": false,
            "position": 4,
            "deleted_at": null,
            "created_at": "2019-10-22 07:16:43",
            "updated_at": "2019-10-22 07:16:43",
            "name": "ASD1",
            "pivot": {
                "product_id": 25,
                "option_id": 23
            },
            "translations": [
                {
                    "id": 23,
                    "option_id": 23,
                    "locale": "en",
                    "name": "ASD1"
                }
            ],
            "values": [
                {
                    "id": 78,
                    "option_id": 23,
                    "price": {
                        "amount": "1.0000",
                        "formatted": "$1.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 07:16:43",
                    "updated_at": "2019-10-22 07:16:43",
                    "label": "A",
                    "translations": [
                        {
                            "id": 78,
                            "option_value_id": 78,
                            "locale": "en",
                            "label": "A"
                        }
                    ]
                },
                {
                    "id": 79,
                    "option_id": 23,
                    "price": {
                        "amount": "2.0000",
                        "formatted": "$2.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-22 07:16:43",
                    "updated_at": "2019-10-22 07:16:43",
                    "label": "B",
                    "translations": [
                        {
                            "id": 79,
                            "option_value_id": 79,
                            "locale": "en",
                            "label": "B"
                        }
                    ]
                },
                {
                    "id": 80,
                    "option_id": 23,
                    "price": {
                        "amount": "3.0000",
                        "formatted": "$3.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 2,
                    "created_at": "2019-10-22 07:16:43",
                    "updated_at": "2019-10-22 07:16:43",
                    "label": "C",
                    "translations": [
                        {
                            "id": 80,
                            "option_value_id": 80,
                            "locale": "en",
                            "label": "C"
                        }
                    ]
                }
            ]
        },
        {
            "id": 24,
            "type": "dropdown",
            "is_required": false,
            "is_global": false,
            "position": 5,
            "deleted_at": null,
            "created_at": "2019-10-22 07:53:36",
            "updated_at": "2019-10-22 07:53:36",
            "name": "A",
            "pivot": {
                "product_id": 25,
                "option_id": 24
            },
            "translations": [
                {
                    "id": 24,
                    "option_id": 24,
                    "locale": "en",
                    "name": "A"
                }
            ],
            "values": [
                {
                    "id": 81,
                    "option_id": 24,
                    "price": {
                        "amount": "3.0000",
                        "formatted": "$3.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-22 07:53:36",
                    "updated_at": "2019-10-22 07:53:36",
                    "label": "s",
                    "translations": [
                        {
                            "id": 81,
                            "option_value_id": 81,
                            "locale": "en",
                            "label": "s"
                        }
                    ]
                }
            ]
        },
        {
            "id": 25,
            "type": "radio",
            "is_required": false,
            "is_global": false,
            "position": 6,
            "deleted_at": null,
            "created_at": "2019-10-23 07:47:40",
            "updated_at": "2019-10-23 07:47:40",
            "name": "assadsfsdf",
            "pivot": {
                "product_id": 25,
                "option_id": 25
            },
            "translations": [
                {
                    "id": 25,
                    "option_id": 25,
                    "locale": "en",
                    "name": "assadsfsdf"
                }
            ],
            "values": [
                {
                    "id": 82,
                    "option_id": 25,
                    "price": {
                        "amount": "2.0000",
                        "formatted": "$2.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 0,
                    "created_at": "2019-10-23 07:47:40",
                    "updated_at": "2019-10-23 07:47:40",
                    "label": "a",
                    "translations": [
                        {
                            "id": 82,
                            "option_value_id": 82,
                            "locale": "en",
                            "label": "a"
                        }
                    ]
                },
                {
                    "id": 83,
                    "option_id": 25,
                    "price": {
                        "amount": "3.0000",
                        "formatted": "$3.00",
                        "currency": "USD"
                    },
                    "price_type": "fixed",
                    "position": 1,
                    "created_at": "2019-10-23 07:47:40",
                    "updated_at": "2019-10-23 07:47:40",
                    "label": "add",
                    "translations": [
                        {
                            "id": 83,
                            "option_value_id": 83,
                            "locale": "en",
                            "label": "add"
                        }
                    ]
                }
            ]
        }
    ]
}

В файле Java я использую утилизацию, но я не думаю, что это хороший подход, пожалуйста, направьте меня в правильном направлении.

//OPTIONS
                    if(product.getOptions().size()>0) {
                        for(int i=0;i<product.getOptions().size();i++) {
                            if(product.getOptions().get(i).getType().equalsIgnoreCase("dropdown")) {
                                optionlistSpinner.add(product.getOptions().get(i));
                            }
                            else if(product.getOptions().get(i).getType().equalsIgnoreCase("checkbox")) {
                                optionlistCheckbox.add(product.getOptions().get(i));
                            }
                            else if(product.getOptions().get(i).getType().equalsIgnoreCase("radio")) {
                                optionlistRadio.add(product.getOptions().get(i));
                            }
                            else if(product.getOptions().get(i).getType().equalsIgnoreCase("multiple_select")) {
                                optionlistSpinnerMulti.add(product.getOptions().get(i));
                                addMultiSelectDropdown("", product.getOptions().get(i).getValues());
                            }
                        }
                    }
                    checkbboxAdapter.notifyDataSetChanged();
                    radiobtnAdapter.notifyDataSetChanged();
                    spinnerAdapter.notifyDataSetChanged();
                    multiSelectSpinner.notifyDataSetChanged();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...