• 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

UI Controller (Activity) doesn't see public methods of ViewModel

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've obviously missed something really basic here, but just can't figure out what!

I have a simple ViewModel.   For brevity I've only included one public method here:



I added the dependencies I think I need to the gradle build file for the Module, the second one in the list:


In the MainActivity I reference the view model.
In the MainAcitivity Class:


And in the OnCreate method of MainActivity I use:


When I try and access the public method it isn't available or recognised.   For instance :

throws up a 'cannot resolve method' error in the IDE.

What am I missing here?  Any advice much appreciated.



Just in case it's relevant, the WordsData class and its method so far is also quite basic:


Interestingly, if I put the two methods in the WordsData class and call make MainActivity call GetAWord directly from the WordsData class it works just fine.  So it looks like it's some requirement of ViewModel that I've missed?
 
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Which public method are you not seeing?

That looks like Android, and I don't know any Android, so I shall move you to our Android forum.
 
Saloon Keeper
Posts: 10762
229
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch.

GetAWord() is a method of GameViewModel, but you've declared your field as a ViewModel.
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it the GetAWord() method you are having trouble with? Does that method override a method in the ModelView class? If not, then the compiler will recognise that ModelView doesn't have that particular method and it won't compile. You will probably get it to work if you declare thet variable as GameModelView.
I don't like that class name, I am afraid, because it is too vague; there are thousands of games requiring model views. I am slo dubious about adding mew methods in subclasses, because you can get the very problem you are describing.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch.

GetAWord() is a method of GameViewModel, but you've declared your field as a ViewModel.



Doh!  I knew it would be something really simple that I'd overlooked.

Thank you Stephan.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Welcome to the Ranch

Which public method are you not seeing?

That looks like Android, and I don't know any Android, so I shall move you to our Android forum.



Thanks Campbell.  It was all of them, but Stephan's correction has got me back on track.
 
Ged Mead
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote: I don't like that class name, I am afraid, because it is too vague; there are thousands of games requiring model views..



I'm completely new to Android and Java.  Just so that I understand, are you saying that other running apps could access a class instance of that name in my app?  

Is there advice on class naming in android java you could point me to ?

Thanks.
 
Campbell Ritchie
Marshal
Posts: 66189
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the game called? If you had a game called JBHIOY nobody would play it because they can't pronounce it, but you might call the class JbhioyModelView. Or is it JbhioyViewModel? View first in the name, it seems
 
What does a metric clock look like? I bet it is nothing like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!