I am trying to pass a string(locationSet) set by a spinner from one activity to another, they both implement an Asynchtask within each class.
I am unable to pass the value from the MainActivity to my WeatherActivity. Using the log, I can see the initial value being set fine in the MainActivity, but null any time I try to pass it to the Weather Activity. If I enter the relevant string within the WeatherActivity manually, the output works as expected.
I have looked through other problems, but I am finding it difficult to apply anything I see.
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());
}
}
}