1

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.

Mark Elliot
  • 75,278
  • 22
  • 140
  • 160
thathashd
  • 1,022
  • 4
  • 17
  • 49

5 Answers5

3

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.

James Black
  • 41,583
  • 10
  • 86
  • 166
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().

Guillaume Brunerie
  • 4,676
  • 3
  • 24
  • 32
  • the "getSomething()" could be for instance a fetchAllData() from the database? – thathashd Apr 13 '11 at 00:54
  • No, the “getSomething()” is a method of the class [Bundle](http://developer.android.com/reference/android/os/Bundle.html). But you can put the key in the bundle, and retrieve the data you need from the database in your second activity. – Guillaume Brunerie Apr 13 '11 at 01:06
  • can you show me some code or some tutorials that shows me how to do that? – thathashd Apr 13 '11 at 12:42
1

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); 
Samuel
  • 9,883
  • 5
  • 45
  • 57
0

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.

Dave MacLean
  • 5,163
  • 2
  • 22
  • 31