将第二微调器传递到其他URL

时间:2019-05-29 01:57:32

标签: android spinner adapter

我有两个微调器,第一个微调器(SpinnerRoute)运行正常,我将选定的值发送给服务器,该服务器将从数据库发送第二个微调器(spinnerStation)的值列表。 我的问题是第二个微调器,我想将选定的值发送到其他URL,我将在下一个活动中使用它。 我希望将选定的微调器发送到set_time.php,但是当我运行代码时,它显示“正在获取数据”,并且当我从spinnerRoute选择一个值时,它将在spinnerStation中显示该值,而不是应用程序停止工作(甚至不显示消息) '单击确定')。 感谢您的帮助。

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_profile);


    spinnerStation= (Spinner)findViewById(R.id.spinnerStation);
    spinnerRoute=(Spinner)findViewById(R.id.spinnerRoute);
    stationList = new ArrayList<Station>();


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

            // Refresh Spinner
            stationList.clear();
            new GetStationFromServer().execute();
        }
        @Override
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });

    //doesn't work
     spinnerStation.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
          @Override
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            // Refresh Spinner
                             routeList.clear();
             stationName = spinnerStation.getSelectedItem().toString();
                                                   new FromServer().execute(stationName);
                                               }

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

        }
    });


}

   private class FromServer extends AsyncTask<String, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProfileActivity.this); //MainActivity
        pDialog.setMessage("click OK");
        pDialog.show();

    }

    @Override
    protected Void doInBackground(String... params) {
        String s = params[0];
        Handler jsonParser = new Handler();
        String json = jsonParser.makeServiceCall("http://192.168.1.2/Android/v1/set_time.php?station="+s, Handler.GET);
        return null ;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        //populateSpinnerStation();
    }

}



private class GetStationFromServer extends AsyncTask<Void, Void, Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(ProfileActivity.this); 
        pDialog.setMessage("Fetching Data");
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        routeName = spinnerRoute.getSelectedItem().toString();



        Handler jsonParser = new Handler();

        String json = jsonParser.makeServiceCall("http://192.168.1.2/Android/v1/get_station.php?route="+routeName, Handler.GET);


        Log.e("Response: ", "> " + json);



        if (json != null) {
            try {

                JSONObject jsonObj = new JSONObject(json);
                if (jsonObj != null) {
                    JSONArray station = jsonObj
                            .getJSONArray("station");

                    for (int i = 0; i < station.length(); i++) {
         JSONObject modObj = (JSONObject) station.get(i);
      Station mod = new Station (modObj.getString("station"));
                        stationList.add(mod);
                    }
                }

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

        } else {
            Log.e("JSON Data", "Didn't receive any data from server!");
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing())
            pDialog.dismiss();
        populateSpinnerStation();
    }
}

private void populateSpinnerStation() {
    List<String> lables = new ArrayList<String>();

    for (int i = 0; i < stationList.size(); i++) {
        lables.add(stationList.get(i).getStation());
    }

    // Creating adapter for spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

    // Drop down layout style - list view with radio button
    spinnerAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinnerStation.setAdapter(spinnerAdapter);
}

0 个答案:

没有答案