Forums Register Login

Passing a class type to a constructor and use it in a cast statement.

+Pie Number of slices to send: Send
I'm trying to understand the best way to organize the two classes below. The sample code, I'm working with, had the DetailFragment as a nested class within ListViewTest. I would like to move DetailFragment into it's own class allowing me to init from any object. The only problem I see is the cast on line 19: ((ListViewTest) getActivity()).doPositiveClick();

What if I wanted to use DetailFragment with a class called ListViewNew? How would I know which class to cast to?



1
+Pie Number of slices to send: Send
Welcome to the Ranch.
I will move your topic over to the Android forum, where Android related questions are discussed
+Pie Number of slices to send: Send
You do it the same way you would if you had multiple implementations in normal classes. You create an interface which contains the method(s) you want to call from the activity. Then you have the activities implement the interface. In the Fragment you cast to the interface, not the activity implementation.
+Pie Number of slices to send: Send
Thank you, this explanation really helped me understand interfaces too!
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1286 times.
Similar Threads
How to check equality of strings ?
How to handle input from a Dialog ?
ArrayList object not passing to String[]
How to get values ?
activity doesn't wait untill alertdialog completes
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 18, 2024 23:31:09.