根据第一个选定的微调器填充多个微调器

时间:2018-06-27 09:39:02

标签: android json

我正在尝试根据从第一个微调器中使用volley的json响应中选择的第一个微调器来填充三个微调器。我已经介绍了几种解决方案,但似乎都无法解决我的问题。这是代码:

json响应

{
    "data": {
        "materials": [
            {
                "id": 1,
                "name": "Test",
                "code": "btedv4",
                "created_at": "2018-06-23 06:08:07",
                "updated_at": "2018-06-23 06:08:07",
                "supplier": [
                    {
                        "id": 1,
                        "name": "Test Supplier",
                        "location": "juja",
                        "created_at": "2018-06-25 06:37:47",
                        "updated_at": "2018-06-25 06:37:47",
                        "pivot": {
                            "material_id": 1,
                            "supplier_id": 1
                        }
                    },
                    {
                        "id": 2,
                        "name": "mlolongo-supplier",
                        "location": "Mlolongo",
                        "created_at": "2018-06-25 06:40:12",
                        "updated_at": "2018-06-25 06:40:12",
                        "pivot": {
                            "material_id": 1,
                            "supplier_id": 2
                        }
                    }
                ],
                "category": [
                    {
                        "id": 1,
                        "material_id": 1,
                        "name": "Machine cut",
                        "status": 1,
                        "created_at": "2018-06-25 06:12:29",
                        "updated_at": "2018-06-25 06:12:29",
                        "quality": [
                            {
                                "id": 1,
                                "category_id": 1,
                                "value": "Premium",
                                "status": 1,
                                "created_at": "2018-06-25 06:18:44",
                                "updated_at": "2018-06-25 06:18:44",
                                "size": [
                                    {
                                        "id": 1,
                                        "quality_id": 1,
                                        "size": "9*9",
                                        "created_at": "2018-06-25 06:19:33",
                                        "updated_at": "2018-06-25 06:19:33",
                                        "price": {
                                            "id": 1,
                                            "material_size_id": 1,
                                            "cost": 37,
                                            "unit_measure": 1,
                                            "created_at": "2018-06-25 06:21:10",
                                            "updated_at": "2018-06-25 06:21:10"
                                        }
                                    }
                                ]
                            }
                        ]
                    }
                ]
            },

Java代码:

一种材料有多个供应商

private void loadSpinnerData(String url) {
            final List<Model_Material> materialsList = new ArrayList<>();
            final List<String> materials_item_list = new ArrayList<>();
            com.android.volley.RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new com.android.volley.Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        if (jsonObject != null) {
                            JSONObject data = jsonObject.getJSONObject("data");
                            JSONArray materials = data.getJSONArray("materials");

                            if (materials != null) {
                                for (int i = 0; i < materials.length(); i++) {

                                    JSONObject material_items = materials.getJSONObject(i);
                                    String material_name = material_items.getString("name");
                                    JSONArray supplier = material_items.getJSONArray("supplier");
                                    /*loop thro suppliers*/

                                    List<String> supplierList = new ArrayList<>();
                                    for (int j = 0; j < supplier.length(); j++) {
                                        JSONObject supplier_items = supplier.getJSONObject(j);


                                        if (supplier != null) {
                                            if (supplierList.contains(supplier.getJSONObject(j).getString("name"))) {


                                            } else {

                                                // store categories to arraylist
                                                supplierList.add(supplier.getJSONObject(j).getString("name"));
                                                // Toast.makeText(getApplicationContext(), "data\n" + supplier.getJSONObject(j).getString("name"), Toast.LENGTH_SHORT).show();


                                            }


                                        }
                                        materialsList.add(new Model_Material(material_name, supplierList));
                                        materials_item_list.add(material_name);

                                    }


                                }
                                final MaterialAdapter materialAdapter = new MaterialAdapter(SellActivity.this,
                                        R.layout.material_list, R.id.spinnerTextItem, materialsList);
                                spinner_sell_material.setAdapter(materialAdapter);

                                spinner_sell_material.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                        Model_Material model_material = materialAdapter.getItem(i);
                                        List<String> supplier_list = model_material.getSupplier();
                                        ArrayAdapter suppliersAdapter = new ArrayAdapter<>(SellActivity.this, R.layout.supplier_list, R.id.supplierSpinnerText, supplier_list);
                                        spinner_sell_supplier.setAdapter(suppliersAdapter);
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> adapterView) {

                                    }
                                });



                            }


                        }


                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new com.android.volley.Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    error.printStackTrace();
                }


            }) {
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String, String> params = new HashMap<String, String>();
                    String bearer = "Bearer ".concat(token);
                    Map<String, String> headersSys = super.getHeaders();
                    Map<String, String> headers = new HashMap<String, String>();
                    headersSys.remove("Authorization");
                    headers.put("Authorization", bearer);
                    headers.putAll(headersSys);
                    return headers;
                }

            };
            MySingleton.getInstance(this).addToRequestQueue(stringRequest);


            int socketTimeout = 30000;
            RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
            stringRequest.setRetryPolicy(policy);
            requestQueue.add(stringRequest);


        }



    //The code for Model_Material is as below:
    public class Model_Material {
        private String material;
        private List<String> supplier;

        public Model_Material(String material, List<String> supplier) {
            this.material = material;
            this.supplier = supplier;
        }

        public String getMaterial() {
            return material;
        }

        public void setMaterial(String material) {
            this.material = material;
        }

        public List<String> getSupplier() {
            return supplier;
        }

        public void setSupplier(List<String> supplier) {
            this.supplier = supplier;
        }
    }


    //The code for the Material Adapter:

    public class MaterialAdapter extends ArrayAdapter<Model_Material> {
        private List<Model_Material> materialList = new ArrayList<>();

        MaterialAdapter(@NonNull Context context, int resource, int spinnerText, @NonNull List<Model_Material> materialList) {
            super(context, resource, spinnerText, materialList);
            this.materialList = materialList;
        }

        @Override
        public Model_Material getItem(int position) {
            return materialList.get(position);
        }

        @NonNull
        @Override
        public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            return initView(position);

        }

        @Override
        public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
            return initView(position);
        }



        private View initView(int position) {
            Model_Material state = getItem(position);
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = inflater.inflate(R.layout.material_list, null);
            TextView textView =  v.findViewById(R.id.spinnerTextItem);
            textView.setText(state.getMaterial());
            return v;

        }

    }

由于第一个微调器上选择的项目没有过滤其他微调器,我该怎么办?

0 个答案:

没有答案