Howto access a string value from another class? - java

I have a class that consists of an edittext box to hold a string "strResponse" I want to access this string in another class. Basically a user types in whatever, and the other class takes that string, and sends it in a text message.
That code works fine if I hard code it in. But I want a user to define what the message should say from the other class.
I was thinking global variable, but have had no luck so far.

what do you mean by hardcode it? could you post your code here?
normally, a decent oop design isn't class 1 "takes" values from class 2, if class 2 is the one which is taking inputs from the user.
The design should be something, class 2 takes input and once input is done it sends/flag/notify class 1 for updated value.
sends/flag/notify could be done by different ways. depends on the design.

I think you need to access the string value of dialog box class to the main Activity class. (Just a for example)
You need interface to do this. i Think link will help
http://developmentality.wordpress.com/2009/10/31/android-dialog-box-tutorial/
In the above link string value in the dialog class is accessed in main activity call using the interface.
Hope this will solve your issue.

Related

Call registerActivityForResult() from non-activity class

So, I want the user to pick an image from the gallery. The user gets the option to do so in multiple activities of app. I don't want to write the code for handling permissions and returning the file uri multiple times and was thinking of putting it all in one class. What would be an appropriate way of doing this? I was thinking of creating a Util class but a normal class doesn't provide me with methods like registerActivityForResult(). How can I do this? And, is there a better way of going about doing this?

Selenium test organisation of classes

Ok selenium gurus a bit of an open question here. I am looking for some guidance on the best way to organize my tests that use object oriented principles.
At the moment I am creating a testrunner main class from which I create an object of a general test class. I am then extending this class for more granular tests.
An example.
I need to open the browser, enter the url, log in as a user.
From there you can access perhaps 40 different links each containing their own pieces of functionality. E.g. A profile link which leads to a profiule screen where you can enter introduction text, upload a picture, change a picture etc...
Another example would be a notification screen where you can navigate to view and mark as read etc...notifications you have received.
I can write the code to test this by for example by creating a ton of methods in that 1 class and then calling these from the main testrunner class. There has to be a better organized way where I can have a separate class for functionality but wont I then have to create a new object for each test?
Sorry about the confused post I'm trying to learn Java thoroughly and selenium also.
EDITED
I have copied the process of creating a page object hybrid model that is documented in the YouTube video:
https://www.youtube.com/watch?v=gxwh8D_tx-0
I created a Pages package which contains all of the Page specific class such such ProfilePage, NotificationPage etc...
I have a second package which contains the tests and a testbase class which generates the driver object, opens the browser.
I want to get to the stage where in my tests class I can have a specific class for a test for example:
class test_that_user_can_upload_profile_picture
When I create such a class I have methods inside the class such as: test_that_navigation_to _profile_page_successful()
test_to_upload_valid_picture()
Should such navigation methods be inside this class?
Also I find that in order to access my methods from a package I need to mark my methods as static. Is this ok? I noticed on the youtube video the instructors methods were not static. Looking at the setup I dont quite understand why I cant access the methods unless I mark them as static. The error i get is
"Cannot make a static reference to a non-static method"
Here is my setup:
Also Im finding that in my ProfilePageNavigation class I have a bunch of methods that run in a specific order based alphabetical order.
Is it simply the case I should just have 1 method in each test class and just call the page classes methods(or any other pertinent class) to execute this test? If it is just 1 method inside each test class then wouldnt I have too many test classes each with a name like (for example) upload_valid_profile_picture with a method using the same name? and then another class with upload_invalid_profile_picture with it's method. I dont want to go down that path - how do I resolve that?
Also all my Pages class methods have to take WebDriver driver as a parameter is there any way around this - it is a lot of duplication.
If you could point me on the right track and let me know in it is ok to have the pages class methods as static it would be appreciated.
I guess I just want to know whether I am on the right track or going down the wrong route at this early stage.
#tarquin - you can find many articles on that #web, There are multiple ways to handle your code and work with it, my way is :
Create a objects repository in notepad or excel, from where you can pick/change/manage all your objects.
Create a class with all re-usable methods.
Create a class of your tests.
Thanks
Keshav

To use a java class methods inside a controller in JavaFXML platform

Problem has been solved
I have a javaFXML application. I want from my controller to use this object:
TempClass tempObject = new TempClass();
I put the above piece of code inside this method
public class FXMLDocumentController implements Initializable
When I run it, it says that this line
TempClass tempObject = new TempClass();
is not found.
Here what I did then but I failed in fixing it anyways:
1- I made sure that the TempClass class is in the same package (in such a way that I can see that my controller and my class are in the same file).
2- I tried to put above it #FXML trying to see if this works but it i did not.
3- I thought that I can just define this class object in the main FXML java file (the one that has the the start method) but the problem is that the IDE underlines every tempObject word in the controller.
So, all what I did is useless.
How can I define it then? Update: Also, if it is applicable, is it a logical problem or a just a technical java problem?
Note1: I am really a beginner in java.
Note2: I think my problem is more to be related to how JavaFXML works than to be related to an OOP misunderstanding but anyways I included the tag: class just in case.
EDIT1: Here does not answer my question in an efficient way and here is why:
1- My question is so specific between a java class and the controller. The question in the link, however, answers a very general case, and indeed, it might answer the case where someone wants to make a communication between two windows or passing parameters to the controller (so an "A" method uses the controller) but my question is that how to let the controller uses my method). With all what I said, I do not deny the fact that this is very related to my question but it is not within the same scope.
2- My level of understanding java is so weak. I know that this is my problem but I am trying to solve the problem, not necessarily the way I want, but the way that can fit my code so that I do not change the whole code. i.e, I keep up with new ways of understanding how Java works and at the same time I try to tolerate my low level of understanding or my weak experience with Java tools and libraries.
EDIT2:
The purpose of my class: My class is just a "flag" class. I am just sending notifications (flags) from my controller's buttons to my class variables. When I click on another button, I need to have these flags so that I know in what state I am.

Make 2 classes share the same instance of an object in Java

I was wondering if anyone could help me. I am stuck with something that seems simple. Say I initialize a Student object with the values of name, age and address in one class. How can I share THAT INSTANCE with another class, eg enable methods in the second class to make changes to the instance, thereby affecting the first class etc.
I was thinking of using JFrames where a new frame would popup and a button would affect values on the first frame.
Thank you
mutators can help you there. or, the singleton pattern, if you want to go "hard-knox".
it's a pattern that allows only one single instance of a class in a jvm to exist.
just look at this
in java if you pass reference of an object to a method and do any changes to that object property, it will change the actual object properties(call by reference)
also if you want to have only single object of a class that needs to be shared across other classes create a singleton pattern

Java : reference one form from another form to append values

In my application I have a main form which has buttons to process other actions, for example my main form displays a list of all entities in my system, and a user is then able to select and then Add or Edit those entities in the application.
When a user presses "Add" a new JFrame is opened, allowing them to add to the system. This is all fine, however I am having a small problem which is affecting some functionality.
I wrote a CommandTracker which allows me to undo and redo operations, the library works fine however I am struggling to reference the CommandTracker object on my main form from other forms. (hopefully code shall convey my question better)
Main Form Declarations:
protected CommandTracker;
// This is instantiated in my constructor, trying to show less code to keep simple.
objCommandTracker = new CommandTracker();
This instantiates my new Command Tracker object within my main form, I would then like to reference this object from my subform so that I can append the next command fired in my sub form.
So far I have tried to resolve this by providing a reference to my main form within my subforms .java file
protected static MainMenu main_menu = null;
I then tried to access the command tracker as follows
// Run AddRequestCommand, through the command tracker
mainMenu.objCommandTracker.executeCommand(addCommand);
However, this results in a null pointer Exception being thrown, I assume this is because mainMenu has a null reference in its declaration. How do I go about passing the current active MainMenu instance to my subform?
Use of statics is never the way to solve something like this. The solution is to pass valid references where needed and to call appropriate methods on these references.
So in other words, if one class needs reference to another, then pass the reference in via a parameter, either a constructor parameter or a setter method parameter, use it to set a field, and then you are free to call methods on that reference in that class.
As an aside: at some other time we'll need to discuss the problems with shooting multiple JFrames at the user, and at your atypical use of the term "form" as if it had a standard meaning in the Swing library -- it doesn't, and so you'll want to use other clearer terms.

Categories

Resources