thathashd
thathashd

Reputation: 1042

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);
        Intent myIntent = new Intent(infoList.this, tabList.class);
        startActivity(myIntent);
    }
});

I also have a ListView. When I click on an item from that ListView I navigate to another activity that shows me the info for that activity. How do I do that? I want that info to correspond to the item I clicked.

Upvotes: 1

Views: 742

Answers (5)

Samuel
Samuel

Reputation: 9993

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

private OnItemClickListener mMessageClickedHandler = new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
    Intent intent = new Intent(this, MyInfoActivity.class);
    intent.putExtra("selected_id", getIdFor(position));
    startActivity(intent);
}
};

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

Upvotes: 1

James Black
James Black

Reputation: 41838

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 = new Intent(myapp, CreateExerciseActivity.class);
            intent.putExtra("selected_id", workout.getId());
            startActivityForResult(intent, CREATE_ACTIVITY);

I then do

            Intent returnIntent = new Intent();
            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.

Upvotes: 3

Dave MacLean
Dave MacLean

Reputation: 5173

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.

Upvotes: 0

Guillaume Brunerie
Guillaume Brunerie

Reputation: 5381

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

Upvotes: 2

Related Questions