• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Problems with Interfaces  RSS feed

 
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My question is inside code:



In other file:
 
Author
Ranch Hand
Posts: 183
9
Java Linux Netbeans IDE Scala Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At present, your code sample has messed up curly braces. Fix those and it might be easier to see what you had in mind
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


other file:
 
Simon Roberts
Author
Ranch Hand
Posts: 183
9
Java Linux Netbeans IDE Scala Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because "j" is a member of the class MainActivity, and does not exist in the OnNewItemAddedListener interface.

In general, I strongly question the design of trying to reach into this thing in this way, but from a purely syntactic perspective, you should be able to cast the OnNewItemAddedListener to a MainActivity (if you're sure it is one!) and then, from the new expression, access the specific features of MainActivity.

E.g.



But please know this a really dreadful approach to any problem. Totally not "good OO". That, however, is a much bigger question, that we probably cannot address here...

HTH
Simon
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why is this even possible:

Is there any rule about this. Because onNewItemAddedListener and Activity are not connected in any way ?

If I wanted to get MainActivity's reference inside onNewItemAddedListener variable I would do simply downcasting(Because MainActivity extends Activity)
 
Bartender
Posts: 2038
273
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

John Losty wrote:Why is this even possible:


Because activity is an instance of a class which implements OnNewItemAddedListener. Also, no need to cast - just specify onNewItemAddedListener = activity;
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Activity doesn't implement OnNewItemAddedListener ?

Would this be correct?
 
Ron McLeod
Bartender
Posts: 2038
273
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - you're right -- I didn't notice that activity was an instance of Activity - you will need the cast - either to MainActivity or more specifically to OnNewItemAddedListener.

     onAttach(Activity activity)
 
John Losty
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Again I am not asking how to fix the code but(let me repeat the question):

Why is this even possible:


Is there any rule about this. Because onNewItemAddedListener and Activity are not connected in any way ?
 
Ron McLeod
Bartender
Posts: 2038
273
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
activity is a reference to an instance of MainActivity, and MainActivity implements OnNewItemAddedListener
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!