首页 > 解决方案 > 在我的 StartActivity 中搜索并在我的 ResultActivitty 中显示结果

问题描述

我是应用程序开发的新手,我遇到了问题。之前我的天气应用程序只有一个活动,搜索和结果在同一个布局文件上。

现在我把它分成了一个 StartActivity,只有一个 EditText 来输入城市名称和一个搜索按钮,并且在结果活动中我已经放置了所有结果文本视图。现在到手头的问题:

当我按下搜索按钮时,它所做的只是切换到我的结果活动,但它不会进行 API 调用或解析 JSON 数据并填充文本视图。

我没有使用 ActionSearch atm,我只是在代表城市的 API 调用中操作字符串。这是我的 StartActivity 类:

公共类 StartActivity 扩展 AppCompatActivity {

static final int REQ = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    configureSearchButton();

}

private void configureSearchButton() {
    Button searchButt = (Button) findViewById(R.id.searchButtonStart);

    final Intent intent = new Intent(StartActivity.this, ResultActivity.class);

    searchButt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivityForResult(intent, REQ);
        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == REQ) {
        if (resultCode == RESULT_OK) {
            String updatedAt = data.getStringExtra("updatedAtKey");
            String tz = data.getStringExtra("tzKey");
            String updatedAtText = data.getStringExtra("updatedAtTextKey");
            String tzText = data.getStringExtra("tzTextKey");
            String temp = data.getStringExtra("tempKey");
            String pressure = data.getStringExtra("pressureKey");
            String humidity = data.getStringExtra("humidityKey");
            String tempMin = data.getStringExtra("tempMinKey");
            String tempMax = data.getStringExtra("tempMaxKey");
            String windSpeed = data.getStringExtra("windSpeedKey");
            String weatherTypeDescription = data.getStringExtra("weatherTypeDescriptionKey");
            String cityCountryAddress = data.getStringExtra("cityCountryAddressKey");;
            String sunrise = data.getStringExtra("sunriseKey");
            String sunset = data.getStringExtra("sunsetKey");
        }
    }
}

这是我的 ResultActivity 代码:


public class ResultActivity extends AppCompatActivity {


    private static final String API_KEY = "blabla";

    TextView cityText, weatherUpdateText, weatherTypeText, temperatureText, minTempText, maxTempText, sunriseText, sunsetText, windSpeedText,
            pressureText, humidityPrcntText, timezoneText;

    Button getLoc;
    private LocationManager locationManager;
    private LocationListener listener;


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cityText = findViewById(R.id.address);
        weatherUpdateText = findViewById(R.id.weatherUpdateAt);
        weatherTypeText = findViewById(R.id.weatherType);
        temperatureText = findViewById(R.id.temperature);
        minTempText = findViewById(R.id.minTemp);
        maxTempText = findViewById(R.id.maxTemp);
        sunriseText = findViewById(R.id.sunrise);
        sunsetText = findViewById(R.id.sunset);
        windSpeedText = findViewById(R.id.windSpeed);
        pressureText = findViewById(R.id.pressure);
        humidityPrcntText = findViewById(R.id.humidity);
        timezoneText = findViewById(R.id.tz);


        final EditText searchEdit = (EditText) findViewById(R.id.cityEdtText);
        final Button submit = (Button) findViewById(R.id.searchButton);

        submit.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View view) {
                new WeatherTask().execute();
            }
        });

        searchEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) || (actionId == EditorInfo.IME_ACTION_DONE)) {
                    submit.performClick();
                }
                return false;
            }
        });

        final SwipeRefreshLayout pullToRefresh = findViewById(R.id.pullToRefresh);


        pullToRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                cityText.setText("CITY,COUNTRY");
                weatherUpdateText.setText("-,-PM");
                weatherTypeText.setText("-");
                temperatureText.setText("-°C");
                minTempText.setText("Min Temp");
                maxTempText.setText("Max Temp");
                sunriseText.setText("-");
                sunsetText.setText("-");
                windSpeedText.setText("-");
                pressureText.setText("-");
                humidityPrcntText.setText("-");
                timezoneText.setText("-");

                new WeatherTask().execute();

                pullToRefresh.setRefreshing(false);

            }
        });

        getLoc = findViewById(R.id.getLocation);

        locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
        listener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                searchEdit.setText(String.valueOf(location.getLongitude()));
            }

            @Override
            public void onStatusChanged(String s, int i, Bundle bundle) {
                //
            }

            @Override
            public void onProviderEnabled(String s) {
                //
            }

            @Override
            public void onProviderDisabled(String s) {
                Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivity(i);
            }
        };

        getLoc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                configure_button();
            }
        });

        configureBackButton();

        Intent data = new Intent();
        data.putExtra("updatedAtKey","updatedAt");
        data.putExtra("tzKey","tz");
        data.putExtra("updatedAtTextKey","updatedAtText");
        data.putExtra("tzTextKey", "tzText");
        data.putExtra("tempKey", "temp");
        data.putExtra("pressureKey", "pressure");
        data.putExtra("humidityKey", "humidity");
        data.putExtra("tempMinKey", "tempMin");
        data.putExtra("tempMaxKey", "tempMax");
        data.putExtra("windSpeedKey", "windSpeed");
        data.putExtra("weatherTypeDescriptionKey", "weatherTypeDescription");
        data.putExtra("cityCountryAddressKey", "cityCountryAddress");
        data.putExtra("sunriseKey", "sunrise");
        data.putExtra("sunsetKey", "sunset");
        setResult(RESULT_OK,data);
        finish();

    }

    private void configureBackButton() {

        Button back = (Button) findViewById(R.id.goBack);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }




    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode) {
            case 10:
                configure_button();
                break;
            default:
                break;
        }
    }

    void configure_button() {
        // first check for permissions
        if (ActivityCompat.checkSelfPermission(ResultActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(ResultActivity.this,
                ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                request_permission();
            }
        } else {
            // permission has been granted
            locationManager.requestLocationUpdates("gps", 5000, 0, listener);
        }
    }

    private void request_permission() {
        if (ActivityCompat.shouldShowRequestPermissionRationale(ResultActivity.this,
                ACCESS_COARSE_LOCATION)) {

            Snackbar.make(findViewById(R.id.mainContainer), "Location permission is needed because ...",
                    Snackbar.LENGTH_LONG)
                    .setAction("retry", new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                                requestPermissions(new String[]{ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 10);
                            }
                        }
                    })
                    .show();
        } else {
            // permission has not been granted yet. Request it directly.
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(new String[]{ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION}, 10);
            }
        }
    }

//FIXA

    class GeocoderHandler extends Handler {

        final EditText searchEdit = (EditText) findViewById(R.id.cityEdtText);

        @Override
        public void handleMessage(Message message) {
            String result;
            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    result = bundle.getString("address");
                    break;
                default:
                    result = null;
            }
            // replace by what you need to do
            searchEdit.setText(result);
        }
    }


    class WeatherTask extends AsyncTask<String, Void, String> {


        protected void onPreExecute() {
            super.onPreExecute();
            findViewById(R.id.loadingCircle).setVisibility(View.VISIBLE);
            findViewById(R.id.mainContainer).setVisibility(View.GONE);
            findViewById(R.id.errorTxt).setVisibility(View.GONE);
        }


        protected String doInBackground(String... args) {

            EditText search = findViewById(R.id.cityEdtText);
            String CITY_COUNTRY = "";

            CITY_COUNTRY = search.getText().toString();

            if (CITY_COUNTRY.matches("")) {

                runOnUiThread(new Runnable() {

                    //Om inputen är tom dyker ett Toast Message upp som förklarar att inputen inte får vara tom och ber användaren att försöka igen.
                    public void run() {
                        Toast toast = Toast.makeText(getApplicationContext(), "Your input cannot be empty, enter a city name.", Toast.LENGTH_LONG);
                        toast.show();
                    }
                });
            }

            String URL = "https://api.openweathermap.org/data/2.5/weather?q=" + CITY_COUNTRY + "&units=metric&appid=" + API_KEY;


            String result = HttpGetRequest.executeHttpGetRequest(URL);

            return result;
        }


        protected void onPostExecute(String result) {

            try {


                JSONObject jsonObj = new JSONObject(result);

                JSONObject main = jsonObj.getJSONObject("main");
                JSONObject sys = jsonObj.getJSONObject("sys");
                JSONObject wind = jsonObj.getJSONObject("wind");
                JSONObject weather = jsonObj.getJSONArray("weather").getJSONObject(0);

                Long updatedAt = jsonObj.getLong("dt");
                Long tz = jsonObj.getLong("timezone");
                String updatedAtText = "Weather updated at: " + new SimpleDateFormat("dd/MM/yyy hh:mm a", Locale.ENGLISH)
                        .format(new Date(updatedAt * 1000));
                String tzText = String.valueOf(tz);
                String temp = main.getString("temp") + "°C";
                String pressure = main.getString("pressure") + " hpa";
                String humidity = main.getString("humidity") + " %";
                String tempMin = "Min Temp \n" + "   " + main.getString("temp_min") + "°C";
                String tempMax = "Max Temp \n" + "   " + main.getString("temp_max") + "°C";
                String windSpeed = wind.getString("speed") + " m/s";
                String weatherTypeDescription = weather.getString("description");
                String cityCountryAddress = jsonObj.getString("name") + ", " + sys.getString("country");
                Long sunrise = sys.getLong("sunrise");
                Long sunset = sys.getLong("sunset");

                cityText.setText(cityCountryAddress);
                weatherUpdateText.setText(updatedAtText);
                weatherTypeText.setText(weatherTypeDescription.toUpperCase());
                temperatureText.setText(temp);
                minTempText.setText(tempMin);
                maxTempText.setText(tempMax);
                sunriseText.setText(new SimpleDateFormat("hh:mm a", Locale.ENGLISH).format(new Date(sunrise * 1000)));
                sunsetText.setText(new SimpleDateFormat("hh:mm a", Locale.ENGLISH).format(new Date(sunset * 1000)));
                windSpeedText.setText(windSpeed);
                pressureText.setText(pressure);
                humidityPrcntText.setText(humidity);
                timezoneText.setText(tzText);


                findViewById(R.id.loadingCircle).setVisibility(View.GONE);
                findViewById(R.id.mainContainer).setVisibility(View.VISIBLE);
            } catch (JSONException e) {

                findViewById(R.id.loadingCircle).setVisibility(View.GONE);
                findViewById(R.id.errorTxt).setVisibility(View.VISIBLE);

                final Handler handler = new Handler();

                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {

                        Intent intent = new Intent(getApplicationContext(), ResultActivity.class);
                        startActivity(intent);

                    }
                }, 5000);
            }
        }
    }


}


标签: androidandroid-activitysearchactivity

解决方案


您可以使用 intent.putExtra() 将数据从 startActivity 传递到 ResultActivity。

startActivity(new Intent(StartActivity.this, ResultActivity.class).putExtra("SearchText",edittext.getText().toString()));

然后在 ResultActivity 的 onCreate() 方法中,您可以像这样获取这些数据,并对其进行处理。

String cityName= context.getIntent().getStringExtra("SearchText");

要再次从 ResultActivity 获取数据到 StartActivity,您可以使用 StartActivityForResult 和 OnActivityResult 方法。

希望这会有所帮助!


推荐阅读