user2163853 user2163853 -4 years ago 107
Android Question

Passing data from one activity to another using bundle - not displaying in second activity

I'm currently trying to take data acquired through a REST API call, parse it for the information I need, and then pass that information to a new activity. I'm using the Asynchronous HTTP Client from loopj.com for the REST client, and then using the below code for my

onClick
and
onCreate
for the current and future activities, respectively.

Eclipse does not pass me any errors for any of my code, but when I try to run in the emulator, I get nothing (i.e. blank white screen) when the new activity/view opens. I've tried to code with a different URL in my REST CLIENT, but I still see nothing. I even took the API call out of the equation by commenting out the try/catch in
onClick
and changing
venueName
in the
bundle.putString("VENUE_NAME", venueName);
to
searchTerm
. Still, the new view comes up but nothing is displayed. What is not being passed, or what am I forgetting to make the second activity show the
venueName
?

public void onClick(View view) {
Intent i = new Intent(this, ResultsView.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String searchTerm = editText.getText().toString();


//call the getFactualResults method
try {
getFactualResults(searchTerm);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);

//Fire the second activity
startActivity(i);
}


Method in second activity that should receive the intent and bundle and display it:

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Get the message from the intent
//Intent intent = getIntent();
//String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

//Get the bundle
Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString(MainActivity.VENUE_NAME);

//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venName);

//set the text view as the activity layout
setContentView(textView);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
}


Thanks for your help. Very much appreciated.

Answer Source

Two ways you can send the data. This is how you are sending it at the moment. And there is nothing wrong with it.

//Create the bundle
Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);

In you code (second Activity) however, you are referring to the key in the Bundle as MainActivity.VENUE_NAME but nothing in the code suggests that you have a class that returns the value as the actual key name send with the Bundle. Change your code in the second Activity to this:

Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString("VENUE_NAME");        

//Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(venName);

You can check in your second Activity if the Bundle contains the key using this and you will know that the key is not present in the Bundle. The correction above, however, will get it working for you.

if (bundle.containsKey(MainActivity.VENUE_NAME))    {
    ....
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download