【问题标题】:How can you change the URL based on item selected in spinner with button click? Android如何通过单击按钮根据微调器中选择的项目更改 URL?安卓
【发布时间】:2019-11-24 20:15:32
【问题描述】:

我有一个带有按钮的活动和另一个在用户单击按钮后打开的活动。我正在尝试根据下拉菜单中选择的内容创建一个新网址,以便在按下按钮后在第二个活动中打开。

第一个活动

 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();

【问题讨论】:

  • intent.putExtra("cityposition", cityPosition);
  • 我将它添加到按钮单击事件中并且它有效,但我只得到第一个值
  • 第一个值是什么意思?
  • cityPosition=0,我会更新我的代码

标签: android android-intent android-spinner openweathermap


【解决方案1】:
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 编码。

【讨论】:

  • 我用上面的代码试过了,它设置为柏林,它忽略了监听器
  • 那么 cityPosition 的值是多少? “它忽略了听众”是什么意思?
  • 在这两个活动中,您都有一个变量 cityPosition。请在第二个活动中展示您如何实例化它。
  • 值为 0,每当我调用 getUrl 代替您拥有城市的位置时,它只会选择第一个,我单击微调器中的另一个选项,它仍然显示来自第一个 cityPosition 的天气
  • 第二个activity没有cityPosition,我会展示我使用的get方法
猜你喜欢
  • 2016-05-19
  • 1970-01-01
  • 1970-01-01
  • 2014-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多