将JsonArrayRequest与setOnItemSelectedListener一起使用

时间:2016-12-23 05:13:24

标签: android arrays android-spinner

我试图在片段中使用spinner.setOnItemSelectedListener调用JsonArrayRequest。但它在这一行上给我错误

RequestQueue requestQueue = Volley.newRequestQueue(this);

我试过

RequestQueue requestQueue = Volley.newRequestQueue(this.getActivity);

  Volley中

newRequestQueue(android.content.Context)无法应用于(匿名)   android.widget.AdapterView.OnItemSelectedListener)

但它没有用。

final Spinner type = (Spinner) rootView.findViewById(R.id.spinner);

type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            final int index = parent.getSelectedItemPosition();
            final String item_type = String.valueOf(index);

            JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, ITEM_CUSTOMER_MEASUREMENTS, null,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {

                            try {
                                JSONObject object = new JSONObject();

                                String shoulder = object.getString("shoulder");
                                String arm_hole = object.getString("arm_hole");
                                String chest_1 = object.getString("chest_1");
                                String chest_2 = object.getString("chest_2");
                                String waist = object.getString("waist");
                                String hip = object.getString("hip");
                                String slit = object.getString("slit");
                                String top_length = object.getString("top_length");
                                String f_neck = object.getString("f_neck");
                                String b_neck = object.getString("b_neck");
                                String sleeve_length = object.getString("sleeve_length");
                                String sleeve_breadth = object.getString("sleeve_breadth");
                                String sleeve_type = object.getString("sleeve_type");
                                String hip_size = object.getString("hip_size");
                                String ankle = object.getString("ankle");
                                String bottom_length = object.getString("bottom_length");
                                String knee = object.getString("knee");
                                String bottom_breadth = object.getString("bottom_breadth");
                                String thigh = object.getString("thigh");
                                String description = object.getString("description");

                                Log.d("response", "onResponse: " +shoulder +arm_hole +chest_1 +chest_2 +hip +slit +top_length +f_neck +b_neck +sleeve_length
                                        +sleeve_breadth +sleeve_type +hip_size +ankle +bottom_length +knee +bottom_breadth +thigh +description);

                                SharedPreferences sharedPreferences = getActivity().getSharedPreferences("itemDetails", Context.MODE_PRIVATE);
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("shoulder", shoulder);
                                editor.putString("arm_hole", arm_hole);
                                editor.putString("chest_1", chest_1);
                                editor.putString("chest_2", chest_2);
                                editor.putString("waist", waist);
                                editor.putString("hip", hip);
                                editor.putString("slit", slit);
                                editor.putString("top_length", top_length);
                                editor.putString("f_neck", f_neck);
                                editor.putString("b_neck", b_neck);
                                editor.putString("sleeve_length", sleeve_length);
                                editor.putString("sleeve_breadth", sleeve_breadth);
                                editor.putString("sleeve_type", sleeve_type);
                                editor.putString("hip_size", hip_size);
                                editor.putString("ankle", ankle);
                                editor.putString("bottom_length", bottom_length);
                                editor.putString("bottom_breadth", bottom_breadth);
                                editor.putString("knee", knee);
                                editor.putString("thigh", thigh);
                                editor.putString("description", description);
                                editor.apply();

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {

                        }
                    }){
                @Override
                public Map<String, String> getHeaders() throws AuthFailureError {
                    Map<String,String> headers = new HashMap<>();
                    headers.put("Authorization", api_token);
                    headers.put("X-Request-ID", cust_id);
                    headers.put("X-Requested-With", item_type);
                    return headers;
                }
            };

            //Creating request queue
            RequestQueue requestQueue = Volley.newRequestQueue(this);

            //Adding request to the queue
            requestQueue.add(jsonArrayRequest);

            final SharedPreferences sharedPrefer = getActivity().getSharedPreferences("itemDetails", Context.MODE_PRIVATE);
            final String arm_hole = sharedPrefer.getString("arm_hole","");
            final String chest_1 = sharedPrefer.getString("chest_1","");
            final String chest_2 = sharedPrefer.getString("chest_2","");
            final String waist = sharedPrefer.getString("waist","");
            final String hip = sharedPrefer.getString("hip","");
            final String slit = sharedPrefer.getString("slit","");
            final String top_length = sharedPrefer.getString("top_length","");
            final String f_neck = sharedPrefer.getString("f_neck","");
            final String b_neck = sharedPrefer.getString("b_neck","");
            final String sleeve_length = sharedPrefer.getString("sleeve_length","");
            final String sleeve_breadth = sharedPrefer.getString("sleeve_breadth","");
            final String sleeve_type = sharedPrefer.getString("sleeve_type","");
            final String hip_size = sharedPrefer.getString("hip_size","");
            final String ankle = sharedPrefer.getString("ankle","");
            final String bottom_length = sharedPrefer.getString("bottom_length","");
            final String bottom_breadth = sharedPrefer.getString("bottom_breadth","");
            final String knee = sharedPrefer.getString("knee","");
            final String thigh = sharedPrefer.getString("thigh","");
            final String shoulder = sharedPrefer.getString("shoulder","");

            switch (index){
                    /*
                    * Nothing is selected
                    * */
                case 0:
                    break;
                    /*
                    * item1 is selected
                    * */
                case 1:
                    break;
                    /*
                    * item2 is selected
                    * */
                case 2:
                    break;
                    /*
                    * item3 is selected
                    * */
                case 3:
                    break;
                    /*
                    * item4 is selected
                    * */
                case 4:
                    break;
                    /*
                    * item5 is selected
                    */
                case 5:
                    break;
                    /*
                    * item6 is selected
                    * */
                case 6:
                    break;
                    /*
                    * item7 is selected
                    * */
                case 7:
                    break;
                    /*
                    * item8 is selected
                    * */
                case 8:
                    /*
                    * item9 is selected
                    * */
                    break;
            }

        }

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

        }
    });

2 个答案:

答案 0 :(得分:5)

  

为什么会出现此错误

如果你研究Volley.newRequestQueue(...)的语法,它需要一个Context类的参数。所以在这种情况下,android.widget.AdapterView.OnItemSelectedListener不是Context类(即不是从Context派生的)。因此,在课程android.widget.AdapterView.OnItemSelectedListener中,您需要引用ActivityActivity from Fragment来传递给方法Volley.newRequestQueue(...)

  

所以你的代码将是

RequestQueue requestQueue = Volley.newRequestQueue(ActivityClassName.this);

RequestQueue requestQueue = Volley.newRequestQueue(FragmentClassName.this.getActivity());
  

最好将应用程序上下文作为

传递

Volley.newRequestQueue(ActivityClassName.this.getApplicationContext())Volley.newRequestQueue(FragmentClassName.this.getActivity().getApplicationContext());

答案 1 :(得分:0)

你应该在onCreateView()方法上初始化它,

        public RequestQueue requestQueue;
        requestQueue = Volley.newRequestQueue(getActivity());