Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Problems with Interfaces

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



In other file:
 
Simon Roberts
Author
Ranch Hand
Posts: 170
7
Java Linux Netbeans IDE
  • 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: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


other file:
 
Simon Roberts
Author
Ranch Hand
Posts: 170
7
Java Linux Netbeans IDE
  • 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: 71
  • 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)
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • 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: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Activity doesn't implement OnNewItemAddedListener ?

Would this be correct?
 
Ron McLeod
Bartender
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • 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: 71
  • 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
Pie
Posts: 1049
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
activity is a reference to an instance of MainActivity, and MainActivity implements OnNewItemAddedListener
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic