如何在微调器上分配嵌套数组数据?

时间:2019-03-27 11:34:28

标签: android arrays json arraylist

我正在调用此数组,但正在获得价值,但无法在微调器上进行赋值。

这是我的数组数据。

  "data": {
                "id": "105",
                "prod_id": "FMSP-6815",
                "prod_cat_id": "4",
                "prod_name": "Aakash Fish",
                "prod_price": "1",
                "is_avlbl": "0",
                "prod_weight": "500,1000",
                "prod_quantity": "0",
                "prod_desc": "Eat at your own risk",
                "prod_image": "medicine.jpg",
                "created_at": "2019-03-26 17:33:26",
                "updated_at": "2019-03-26 17:33:26",
                "bone_type": "0",
                "skin_type": "0",
                "cut_type": "0",
                "cat_name": "ready_to_eat",
                "prod_attributes": [
                    {
                        "id": "15",
                        "name": "Size",
                        "attribute_options": [
                            {
                                "id": "15",
                                "attribute_id": "15",
                                "attribute_name": "size",
                                "prod_id": "105",
                                "attr_option_id": "8",
                                "option_price": "10",
                                "option_name": "Large"
                            }
                        ]
                    },
                    {
                        "id": "14",
                        "name": "Clean",
                        "attribute_options": [
                            {
                                "id": "16",
                                "attribute_id": "14",
                                "attribute_name": "clean",
                                "prod_id": "105",
                                "attr_option_id": "6",
                                "option_price": "5",
                                "option_name": "Skinny"
                            },
                            {
                                "id": "20",
                                "attribute_id": "14",
                                "attribute_name": "Clean",
                                "prod_id": "105",
                                "attr_option_id": "7",
                                "option_price": "30",
                                "option_name": "White Skin"
                            }
                        ]
                    },
                    {
                        "id": "7",
                        "name": "Cut",
                        "attribute_options": [
                            {
                                "id": "18",
                                "attribute_id": "7",
                                "attribute_name": "Cut",
                                "prod_id": "105",
                                "attr_option_id": "4",
                                "option_price": "20",
                                "option_name": "Medium"
                            }
                        ]
                    }
                ]
            }

这是我的数组 和 这就是我从API调用的方式。
这是我的Java代码。我在for循环中使用过for循环。

if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {

                        attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
                        attribute_array_size = attributeData.size();

                        if (attributeData.size() == 0){

                            p_attribute.setVisibility(View.GONE);

                        } else {

                            p_attribute.setVisibility(View.VISIBLE);

                            // Loading attribute options

                            for (int j=0;j<attributeData.size();j++){

                                attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();

                                //String option = attributeOptions.get(j).getOption_name();
                               // Log.e("options",option);
                                dateList.clear();
                                dateList.add("Select");
                                for (int k=0;k<attributeOptions.size();k++){

                                    String attribute_id = attributeOptions.get(k).getAttribute_id();
                                    String option_name = attributeOptions.get(k).getOption_name();
                                    String option_price = attributeOptions.get(k).getOption_price();

                                    dateList.add(option_name);
                                    //Log.e("option_id",attribute_id);
                                    //Log.e("option_name",option_name);
                                    // Log.e("option_price",option_price);
                                }
                                ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(),R.layout.date_dialog, R.id.textDate, dateList);
                                holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
                            }

                        }

                    }

1 个答案:

答案 0 :(得分:1)

根据您提出的问题,我假设您的微调器显示了最后一个值。您的代码有两个问题。

1- dateList.clear();dateList.add("Select");应该在循环之外

2-在将所有值分配给arrayList之后,将微调适配器设置为外部循环

下面您可以查看其运行方式

if (response.body().getDescriptionResponseData().getSuccess().equals("200")) {

            attributeData = response.body().getDescriptionResponseData().getP_data().getAttributeData();
            attribute_array_size = attributeData.size();

            if (attributeData.size() == 0){

                p_attribute.setVisibility(View.GONE);

            } else {

                p_attribute.setVisibility(View.VISIBLE);

                // Loading attribute options

                dateList.clear();
                dateList.add("Select");
                for (int j=0;j<attributeData.size();j++){

                    attributeOptions = response.body().getDescriptionResponseData().getP_data().getAttributeData().get(j).getAttributeOptionsData();

                    //String option = attributeOptions.get(j).getOption_name();
                    // Log.e("options",option);

                    for (int k=0;k<attributeOptions.size();k++){

                        String attribute_id = attributeOptions.get(k).getAttribute_id();
                        String option_name = attributeOptions.get(k).getOption_name();
                        String option_price = attributeOptions.get(k).getOption_price();

                        dateList.add(option_name);
                        //Log.e("option_id",attribute_id);
                        //Log.e("option_name",option_name);
                        // Log.e("option_price",option_price);
                    }
                }

                if (dateList.size() > 0) {
                    ArrayAdapter dateArrayAdapter = new ArrayAdapter(getContext(), R.layout.date_dialog, R.id.textDate, dateList);
                    holder.attribute_option_spinner.setAdapter(dateArrayAdapter);
                }

            }


        }