Skip to content Skip to sidebar Skip to footer

Passing Data Between Activities

I have the following code: chart.setOnClickListener(new OnClickListener() { public void onClick(View v) { final String aux= (String) lt.getItemAtPosition(position);

Solution 1:

Here is what I did in this situation, if you don't want to just pass an id back:

I call the other activity with this:

            Intent intent = newIntent(myapp, CreateExerciseActivity.class);
            intent.putExtra("selected_id", workout.getId());
            startActivityForResult(intent, CREATE_ACTIVITY);

I then do

IntentreturnIntent=newIntent();
            returnIntent.putExtra("name", m.getName());
            returnIntent.putExtra("unit", m.getUnit());
            returnIntent.putExtra("quantity", m.getQuantity());
            if (getParent() == null) {
                setResult(Activity.RESULT_OK, returnIntent);
            } else {
                getParent().setResult(Activity.RESULT_OK, returnIntent);
            }
            finish();

So, in this case I was passing in an id in order to get more details from the user, and then I pass those back to the calling activity, in order to add it, as I didn't want to save this until the user chooses to later.

So, you need to do startActivityForResult in order to have it able to return the data.

Solution 2:

You can add some data to the Intent with the methods putExtra(), and then retrieve the data in the new Activity with getExtras().getSomething().

Solution 3:

i guess you have to use OnItemClickListener instead of the click event and you need intent to call the next activity.

privateOnItemClickListenermMessageClickedHandler=newOnItemClickListener() {
publicvoidonItemClick(AdapterView parent, View v, int position, long id)
{
    Intentintent=newIntent(this, MyInfoActivity.class);
    intent.putExtra("selected_id", getIdFor(position));
    startActivity(intent);
}
};

mHistoryView = (ListView)findViewById(R.id.history);
mHistoryView.setOnItemClickListener(mMessageClickedHandler); 

Solution 4:

Possible answer: How do I pass data between Activities in Android application?

http://thedevelopersinfo.wordpress.com/2009/10/15/passing-data-between-activities-in-android/

Solution 5:

It really depends on what the data is in your listview. For example, if you're displaying a list of contacts in the listview, you could just pass the ID of the contact over to the other activity, and let that activity access the content provider for contacts to retrieve the data you want it to work with. You'd pass an ID within a URI in the data field of the intent, as opposed to in the extras bundle.

Post a Comment for "Passing Data Between Activities"