I have a custom adapter that I'm using to populate a ListView. I have an onItemClick()
set on the ListView item that changes the displayed fragment to something else.
I want to pass data to the new fragment from the ListView item, but I don't want to get any of that data from the visible views if I can help it. The custom adapter parses a JSONObject to get the data to populate the ListView. So is there a way that I can access that underlying data outside of the adapter class?
More specifically, I'm trying to put the necessary underlying ListView data into a Bundle and pass that Bundle from the ListView item up to the activity, to then pass into the new fragment.
I also want to get it from the getView()
method so I don't have to duplicate code in another custom public method within the adapter.
You can do that if your custom adapter overrides the getItem(int position)
method and there you should return the object at the given position within your adapter's dataset.
After that you can get the clicked data using onItemSelected(AdapterView<?> parent, View view, int position, long id)
on the ListView
by calling this:
parent.getAdapter().getItem(position)
-> and this is your data corresponding to your clicked item.
You just have to cast that returned object to whatever your class is and do what you want to it..
good luck and I hope it helps!