Android Asynctasks,在活动之间传递String

时间:2013-08-19 03:55:40

标签: android android-asynctask

我试图将一个微调器设置的字符串( locationSet )从一个活动传递到另一个活动,它们都在每个类中实现一个Asynchtask。

我无法将MainActivity中的值传递给WeatherActivity。使用日志,我可以看到MainActivity中的初始值设置得很好,但是每当我尝试将其传递给Weather Activity时都会为null。如果我手动输入WeatherActivity中的相关字符串,则输出按预期工作。

我已经看过其他问题,但我发现很难应用我看到的任何东西。

MainActivity.java

public class MainActivity extends Activity {

Button searchLocation;
Spinner locationSpinner;
String locationSet = null;
String strLocation = null;
Locations arrLocation;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    searchLocation = (Button) findViewById(R.id.searchLocationButton);

    locationSpinner = (Spinner) findViewById(R.id.locationsListView);

    ArrayAdapter<CharSequence> adaptor = ArrayAdapter.createFromResource(this, R.array.location_array, android.R.layout.simple_list_item_1);
    locationSpinner.setAdapter(adaptor);

    searchLocation.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            new MyAsync().execute();

        }
    });
}

public void onBackPressed() {
    super.onBackPressed();
    this.finish();
}

class MyAsync extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {

        strLocation = locationSpinner.getSelectedItem().toString();

        if (strLocation.equals("Aberdeen")) {
            locationSet = arrLocation.ABERDEEN;
        } else if (strLocation.equals("Dundee")) {
            locationSet = arrLocation.DUNDEE;
        } else if (strLocation.equals("Edinburgh")) {
            locationSet = arrLocation.EDINBURGH;
        } else if (strLocation.equals("Fort William")) {
            locationSet = arrLocation.FORT_WILLIAM;
        } else if (strLocation.equals("Glasgow")) {
            locationSet = arrLocation.GLASGOW;
        } else if (strLocation.equals("Manchester")) {
            locationSet = arrLocation.MANCHESTER;
        } else if (strLocation.equals("North Berwick")) {
            locationSet = arrLocation.NORTH_BERWICK;
        } else if (strLocation.equals("Portree")) {
            locationSet = arrLocation.PORTREE;
        } else if (strLocation.equals("Ullapool")) {
            locationSet = arrLocation.ULLAPOOL;
        }

        Intent intent = new Intent(MainActivity.this, WeatherActivity.class);
        startActivity(intent);
    }
}

}

WeatherActivity.java

    public class WeatherActivity extends MainActivity {

    TextView firstDayTextView;

    String selectedLocation = null;

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

        locationTextView = (TextView) findViewById(R.id.locationTextView);



        new LoginAsync().execute();
    }

    class LoginAsync extends AsyncTask<Void, Void, Void> {
        MySaxHandler myHandler;

        StringBuilder builder = new StringBuilder();

        @Override
        protected Void doInBackground(Void... params) {
            myHandler = new MySaxHandler();
            myHandler.addLocation(selectedLocation.toString());
            myHandler.get();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {

            ArrayList<ItemData> items = myHandler.items;

            if (null != items && items.size() != 0) {
                for (int index = 0; index < items.size(); index++) {
                    ItemData obJPost = items.get(index);
                        builder.append("\n" + obJPost.getItemTitle());
                }
            }
            firstDayTextView.setText(builder.toString());

        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以在开始天气活动时使用Intent.putExtra(“TAG”,locationSet)传递字符串值“locationSet”。在onCreate中的天气活动中,您可以使用Intent.getExtra获取值。

代码示例:

Intent i = new Intent(FirstScreen.this, SecondScreen.class);   
String keyIdentifer  = null;
i.putExtra("STRING_I_NEED", strName);

Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
    extras = getIntent().getExtras();
    if(extras == null) {
        newString= null;
    } else {
        newString= extras.getString("STRING_I_NEED");
    }
} else {
    newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}