根据第一个微调器项更新第二个微调器

时间:2016-06-02 11:32:53

标签: android

我在xml中创建了两个微调器,即myspinner和myspinner1。 myspinner值是从json值获得的。基于选择的myspinner项目,我们获取酒店id。检索到的酒店id用于调用另一个AsyncT1。在AsyncT1中,myspinner1值被列出。我的问题是,如果我选择myspinner myspinner1值没有更新它保持不变。我已经尝试过adapter.notifydatasetchanged()但直到现在还没有更新。

class AsyncT extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
            String userID = prefs.getString("userid", null);
            System.out.println("userID---"+userID);

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://airbnb.abservetech.com/demo/public/mobile/hotelroom/hotel");

            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("user_id", userID));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());


         /* execute */

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                InputStream inputStream = response.getEntity().getContent();
                InputStreamToStringExample str = new InputStreamToStringExample();
                responseServer = str.getStringFromInputStream(inputStream);
                Log.d("response", "response -----" + responseServer);

                //Get the instance of JSONArray that contains JSONObjects
                // postData(responseServer);

                JSONObject jsonRootObject = new JSONObject(responseServer);
                JSONArray jsonArray = jsonRootObject.optJSONArray("Hotel_details");

                //Iterate the jsonArray and print the info of JSONObjects
                for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                    try {


                        JSONObject jsonObject = jsonArray.getJSONObject(i1);

                        Movie movie = new Movie();

                        movie.setHotelid1(jsonObject.optString("hotel_id").toString());
                        movie.setHotelname1(jsonObject.optString("hotel_name").toString());
                        hotelNames.add(jsonObject.optString("hotel_name").toString());
                        movieList.add(movie);


                        System.out.println("movie list" + movieList);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                Adapter =  (new ArrayAdapter<String>(Edit_room.this, android.R.layout.simple_spinner_dropdown_item, hotelNames));
                                mySpinner.setAdapter(Adapter);
                                Adapter.notifyDataSetChanged();

                                mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                                    @Override
                                    public void onItemSelected(AdapterView<?> arg0,
                                                               View arg1, int position, long arg3) {
                                        // TODO Auto-generated method stub
                                        // Locate the textviews in activity_main.xml
                                        // Set the text followed by the position
                                        hotelid= (movieList.get(position).getHotelid1());
                                        System.out.println("name--"+hotelid);


                                        new AsyncT1().execute(hotelid);

                                         }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> arg0) {
                                        // TODO Auto-generated method stub
                                    }
                                });
                            }
                        });


                    }
                    catch (JSONException e) {

                        e.printStackTrace();
                    }


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);


        }
    }

class AsyncT1 extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {

          //  http://airbnb.abservetech.com/demo/public/mobile/hotelroom/roomid?hotel_id=79

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://airbnb.abservetech.com/demo/public/mobile/hotelroom/show");
            SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE);
            String userID = prefs.getString("userid", null);
            System.out.println("userID---"+userID);

            /*hotel_id&room_type&room_prize&is_active&available_status&adults_count&room_count&room_images&
                    room_desc&room_name&room_footage&General&Services&Food_Beverages&Outdoors&Activities&
                    Dining&Media_Entertainment&Kitchen&Others*/
            try {
                // Add your data
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                nameValuePairs.add(new BasicNameValuePair("hotel_id", hotelid));

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());


         /* execute */

                // Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                InputStream inputStream = response.getEntity().getContent();
                InputStreamToStringExample str = new InputStreamToStringExample();
                responseServer = str.getStringFromInputStream(inputStream);
                Log.d("response", "viewroomresponse -----" + responseServer);

                JSONObject jsonRootObject = new JSONObject(responseServer);
                JSONArray jsonArray = jsonRootObject.optJSONArray("view_room_details");

                //Iterate the jsonArray and print the info of JSONObjects
                for (int i1 = 0; i1 < jsonArray.length(); i1++) {
                    try {


                        JSONObject jsonObject = jsonArray.getJSONObject(i1);


                        Movie movie = new Movie();


                       // movie.setViewRoomtype(jsonObject.optString("room_type").toString());
                        movie.setViewRoomprice(jsonObject.optString("room_prize").toString());
                        movie.setViewIsActive(jsonObject.optString("is_active").toString());
                        movie.setViewAvailablestatus(jsonObject.optString("available_status").toString());
                        movie.setViewAdultcount(jsonObject.optString("adults_count").toString());
                        movie.setViewRoomcount(jsonObject.optString("room_count").toString());
                        movie.setViewRoomdesc(jsonObject.optString("room_desc").toString());
                        movie.setViewRoomname(jsonObject.optString("room_name").toString());
                        movie.setViewRoomfootage(jsonObject.optString("room_footage").toString());
                        RoomNames.add(jsonObject.optString("room_type").toString());
                        movieList.add(movie);




                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
//                                ArrayAdapter<String> Adapter =  new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1,RoomNames);
//                                Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//                                Adapter.notifyDataSetChanged();
//                                mySpinner1.setAdapter(Adapter);



                                Adapter1 =(new ArrayAdapter<String>(Edit_room.this, android.R.layout.simple_spinner_dropdown_item, RoomNames));
                                if(Adapter1!=null){
                                    Adapter1.notifyDataSetChanged();

                                    mySpinner1.setAdapter(Adapter1);

                                }
                                else{ mySpinner1.setAdapter(Adapter1);}


                                mySpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

                                    @Override
                                    public void onItemSelected(AdapterView<?> arg0,
                                                               View arg1, int position, long arg3) {
                                        // TODO Auto-generated method stub
                                        // Locate the textviews in activity_main.xml
                                        // Set the text followed by the position

                                        hotelid= (movieList.get(position).getHotelid1());

                                        room_price.setText(movieList.get(position).getViewRoomprice());
                                        description.setText(movieList.get(position).getViewRoomdesc());
                                        adult_count.setText(movieList.get(position).getViewAdultcount());


                                  }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> arg0) {
                                        // TODO Auto-generated method stub
                                    }
                                });
                            }
                        });

                    }
                    catch (JSONException e) {

                        e.printStackTrace();
                    }


                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String aVoid) {
            super.onPostExecute(aVoid);


        }
    }

`

1 个答案:

答案 0 :(得分:0)