1

I'm displaying a ListView that I filled with a custom ListAdapter, called LazyAdapter, which code comes from here : http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/. The LazyAdapter uses my custom objects "EventData". I managed to display my List as I wanted, but now I'd like to get the clicked object. I found some help on other questions (here, here, and there), and wrote that code:

private OnItemClickListener mEventClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> av, View v, int arg2, long arg3) {

        EventData selectedEvent = (EventData) eventsListView.getItemAtPosition(arg2);

};

However, I get a cast error at execution.

05-14 17:10:46.718: E/AndroidRuntime(3863): java.lang.ClassCastException: java.lang.Integer

I also tried this similar solution:

EventData selectedEvent = (EventData) av.getItemAtPosition(arg2);

I got the same cast error. I don't know how I can get my Object EventData in an other way.

Thanks for your help!

Community
  • 1
  • 1
  • Post your Adapter code – Pragnani May 14 '13 at 15:26
  • Agreed, it seems like your Adapter's `getItem()` is returning an unexpected value. (Ultimately, ListView's `getItemAtPosition()` is just calling through to your Adapter's `getItem()` function. – Scott W May 14 '13 at 15:34
  • How do you fill/create your LazyAdapter? It seams that the ArrayList you gives to your adapter is an array of integers. – L. G. May 14 '13 at 15:34

3 Answers3

1

In the adapter class, in the method getItem(int position) you should return the EventData object for that position (not the position).

You may have returning Position value in getItem instead of EventData.

Pragnani
  • 20,075
  • 6
  • 49
  • 74
bogdan
  • 782
  • 3
  • 7
1

In your custom list adapter you return an integer in getItem(); so you will receive ClassCastException

mEventData is list which is supplied to CustomListView

ArrayList<EventData> mEventData;

@Override
public Object getItem(int position) {
    return mEventData.get(position);
}
Adrian Viegas
  • 58
  • 1
  • 8
Pawan Yadav
  • 1,772
  • 19
  • 17
0

In my code fetching the object I do

public void onItemClick(AdapterView<?> appList, View arg1,
                int element, long arg3) {
            // Get AppData for demo clicked
            AppData item = demoList.get(element);

where demoList is the underlying data structure (ArrayList < AppData>) bound to the adapter

rcbevans
  • 7,101
  • 4
  • 30
  • 46