1

I am calling this array and I am getting value but I am not able to assign on spinner.

This is my array data.

  "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"
                            }
                        ]
                    }
                ]
            }

This is my array and this is how I am calling from API.
This is my Java code. I used for loop inside for loop.

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 Answer 1

1

From the question you asked I assume that your spinner shows last values. There are two problems with your code.

1- dateList.clear(); and dateList.add("Select"); should be outside for loop

2- Set spinner adapter outside for loop after all values are assigned to arrayList

Below you can check how it will go

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

            }


        }
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for help. Where should i break my for loop. becouse my Size attribute has 1 value and Clean attribute has 2 value and so on.. Please tell me where i break my for loop.
You have size and clean attributes either you have to use two separate spinners or if one spinner then you should know the point to break the loop.
I am using Recycerview to displaying them so i have only one spinner, Please tell me where i point break. if i am pointing break in 2nd loop the it is not loading full data.
You want to load the spinner with both attributes (clean and size) ?
I am getting attribute dynamically and its options, so i have to load Size attribute with its option in one spinner and clean attribute with its option in second spinner.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.