3

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

        }
    }
}
  • 1
    You forgot to `intent.putExtra("STRING_I_NEED", locationSet);` from your `MainActivity` to your `WeatherActivity`. Make sure you get the intent form your `WeatherActivity` and retrieve the string with the same ID you sent, in this case is `STRING_I_NEED`. Hope this helps. – user2652394 Aug 19 '13 at 04:03
  • 1
    no need to use asynctask in MainActivity. just put value in intent and get it back in WeatherActivity. – Ketan Ahir Aug 19 '13 at 04:04
  • Thanks for the response, I have said below, the solution has improved one thing, though not resolved everything. – TheBluePotter Aug 19 '13 at 04:56

1 Answers1

2

You can pass your string value "locationSet" using Intent.putExtra("TAG", locationSet) while starting your weather activity. In weather activity in onCreate you can get the value using Intent.getExtra.

Code example:

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");
}
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • Thanks for the response, now my log in the WeatherActivy shows a value, but I am confused to why its crashing when I switch activities. I am confused to what errors I may have in my code? – TheBluePotter Aug 19 '13 at 04:55
  • Can you please add crash log here? Or can you tell in whihc line is it crashing? – Sushil Aug 19 '13 at 04:59
  • I have added a copy of the log above, I hope this is enough information. I can see straight off that the URL should have a string(representing a location) where the null is. – TheBluePotter Aug 19 '13 at 05:12
  • in WeatherActivity, you take the string locationSet in a "test" String. In doInBackGround(), you are adding "locationSet" to handler. You should add "test". In any case, it looks to be some other error now and not related to your intial problem :) – Sushil Aug 19 '13 at 05:20
  • I've resolved it, yes I noticed that, I didn't add the change here. Thanks for your help. selectedLocation.toString(), I never had .toString() I feel silly, but so relieved its working. – TheBluePotter Aug 19 '13 at 05:44
  • Happy that it worked for you :). Please do accept the answer if you feel it helped you to resolve issue :). Will be easier for others to know the correct answer. Njoy coding :) – Sushil Aug 19 '13 at 05:48
  • Minus the error with regards toString(), its as was before. Thanks again. – TheBluePotter Aug 19 '13 at 06:26