您如何基于通过单击按钮在微调器中选择的项目来更改URL? Android

问题描述 投票:0回答:1

我有一个带有按钮的活动,而另一个活动在用户单击按钮后打开。我试图创建一个新的URL,一旦根据下拉菜单中的选择按下了按钮,就将在第二个活动中打开该URL。

第一个活动

 btn_forecast.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
            Log.i(ACTIVITY_NAME, "User clicked Start Forecast");
            Intent intent = new Intent(StartActivity.this,
                   WeatherForecastActivity.class);
            startActivityForResult(intent, 50);
        }});

  ArrayAdapter<String> myAdapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1, getResources().getStringArray(R.array.item_entries));
        myAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
        selectItem.setAdapter(myAdapter);

        selectItem.setOnItemSelectedListener(
                new AdapterView.OnItemSelectedListener() {

                    @Override
                    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

                        switch (position) {
                            case 0:
                                Toast.makeText(StartActivity.this, "Item one", Toast.LENGTH_SHORT).show();
                                cityPosition=0;
                                break;
                            case 1:
                                Toast.makeText(StartActivity.this, "Item two", Toast.LENGTH_SHORT).show();
                                cityPosition=1;
                                break;
                            case 2:
                                Toast.makeText(StartActivity.this, "Item three", Toast.LENGTH_SHORT).show();
                                cityPosition=2;
                                break;
                            case 3:
                                Toast.makeText(StartActivity.this, "Item four", Toast.LENGTH_SHORT).show();
                                cityPosition=3;
                                break;
                        }
                    }
                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }

                }
        );
    }

单击按钮后将打开第二个活动,其中包含URL

 URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=city,ca&APPID=&mode=xml&units=metric");

                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
                conn.setReadTimeout(10000);
                conn.setConnectTimeout(15000);
                conn.setRequestMethod("GET");
                conn.setDoInput(true);
                conn.connect();

如何根据微调器在第一个活动中选择的内容来更改q中的值?更新

public class Weather{
 public  Integer cityPosition=0;
  btn_forecast.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
            Log.i(ACTIVITY_NAME, "User clicked Start Forecast");
            Intent intent = new Intent(StartActivity.this,
                    WeatherForecastActivity.class);
            startActivityForResult(intent, 50);

                intent.putExtra("cityposition", cityPosition);
        }});
    public String getURL(){

String city ="vancouver";
        if(cityPosition==0) {


            city = "ottawa";


              }

        else if(cityPosition==1){

            city ="vancouver";

        }

        return city;
    }

第二活动

StartActivity start;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_forecast);
start = new StartActivity();

         ForecastQuery f = new ForecastQuery();
        f.execute();

    }

    private class ForecastQuery extends AsyncTask<String, Integer, String> {




        @Override
        protected String doInBackground(String... strings) {
            try {

                URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + start.getURL() + ",ca&APPID=mode=xml&units=metric");
                HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
android android-intent android-spinner openweathermap
1个回答
0
投票
String city = "Berlin";

if ( cityPosition == 1 )
  city = "Paris";

if ( cityPosition == 2 )
  city = "Madrid";

//依此类推,您可能会有一个字符串数组

URL url = new URL("https://api.openweathermap.org/data/2.5/weather?q=" + city + ",ca&APPID=&mode=xml&units=metric");

如果城市名称中有空格,则必须对查询字符串进行url编码。

© www.soinside.com 2019 - 2024. All rights reserved.