Mick Byrne
Mick Byrne

Reputation: 14494

Get specific item property on onListItemClick() using custom ArrayAdapter<>

I have a ListActivity that displays a list of search results I grab from a webservice off the internet. I parse the XML I receive into an ArrayList<MyObjects> which I then bind to a ListView using my own adapter (as in MyObjectAdapter extends ArrayAdapter<MyObject>).

So then I want the user to be able to click on one of the items in the list. Each item has an identifier which will then be put into an intent and sent to a new activity (which then triggers a new webservice request based on this, downloads the rest of the data). But I don't know how to get this one property of the MyObject that was selected.

Here's the onListItemClick() method as it stands:

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
  super.onListItemClick(l, v, position, id);

  String myObjectId;

  // I want to get this out of the selected MyObject class here

  Intent i = new Intent(this, ViewObject.class);
  i.putExtra("identifier_key", myObjectId);
  startActivityForResult(i, ACTIVITY_VIEW);
}

Upvotes: 1

Views: 4018

Answers (2)

Pasha
Pasha

Reputation: 2425

try this getIntent().getExtras().getString(key);

Upvotes: 0

Cristian
Cristian

Reputation: 200150

If you are using ArrayAdapter you must initialize that adapter with an array, right? So, the better you can do is to use the postition that you get from onListItemClick and take the object from the original array.

For instance:

// somewhere you have this
ArrayList<MyObjects> theItemsYouUsedToInitializeTheArrayAdapter;

// and inside onListItemClick....
String myObjectId = theItemsYouUsedToInitializeTheArrayAdapter.get(position).getObjectId();

Upvotes: 3

Related Questions