Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

json

android

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.

like image 478
user2163853 Avatar asked Mar 16 '13 03:03

user2163853


People also ask

How do I transfer data from one activity to another second activity?

We can send the data using putExtra() method from one activity and get the data from the second activity using the getStringExtra() method.

How do you pass data between bundles using fragments?

Therefore, in order to pass your data to the Fragment being created, you should use the setArguments() method. This methods gets a bundle, which you store your data in, and stores the Bundle in the arguments. Subsequently, this Bundle can then be retrieved in onCreate() and onCreateView() call backs of the Fragment.


4 Answers

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))    {
    ....
}
like image 172
Siddharth Lele Avatar answered Oct 24 '22 16:10

Siddharth Lele


I think if you replace

String venName = bundle.getString(MainActivity.VENUE_NAME); 

with

String venName = bundle.getString("VENUE_NAME");

it should work.

Here is how I handle my transfer of data from one activity to another:

Sending data to activity called "Projectviewoptions":

Bundle b = new Bundle();
          b.putString("id", str_projectid);
          Projectviewoptions pv = new Projectviewoptions();

Receiving data:

idbundle = getArguments();
String myid = idbundle.getString("id");

The "key" on both sides should be same; in this case "id".

Another way to send data, through intent is:

Send:

Intent intent = new Intent(getActivity(),ViewProjectDetails.class);
                            intent.putExtra("id", myid);
                            startActivity(intent);

Recieve:

String id = getIntent().getExtras().getString("id");
like image 32
krtkush Avatar answered Oct 24 '22 18:10

krtkush


You are wrongly accessing the key which you have added in bundle. Besides getting the String as MainActivity.VENUE_NAME try to directly pass the key name which you have added in bundle as below:

Besides getting string as below :

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

Try to get the string using key name as below:

    /Get the bundle
     Bundle bundle = getIntent().getExtras();
    //Extract the data…
   String venName = bundle.getString("VENUE_NAME");  
like image 26
GrIsHu Avatar answered Oct 24 '22 18:10

GrIsHu


to send bundle.

Bundle bundle = new Bundle();
bundle.putString("Name",Object); //This is for a String
i.setClass(CurrentClass.this, Class.class);
i.putExtras(bundle);
startActivity(i);

to receive bundle

Bundle bundle = null;
bundle = this.getIntent().getExtras();
String myString = bundle.getString("Name");//this is for String  
like image 20
Andre Viau Avatar answered Oct 24 '22 16:10

Andre Viau