I wrote a program to work from the console in Eclipse and the terminal window in Linux. I am now transforming it into an Android app and I have the basic functionality of the Android UI done up until the point where it needs to use the logic from the Java file of the program I wrote. All of my inputs from the Java file are currently from the keyboard (from Scanners).
My question is: how do I transform this to get it work with the user interaction of the app?
The only input would be from the built in NumberPicker. The Java file starts from the main method: public static void main(String[] args) {)
For example:
Sample Android Code:
public class Source1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.source1_layout);
Intent intent = getIntent();
int pickerValue = intent.getIntExtra("numEntered", 0);
}
Sample Java Code from program (Test.java):
public class Test {
public static void main(String[] args) {
System.out.println("Enter number: ");
Scanner reader = new Scanner(System.in);
int num = reader.nextInt();
...
}
}
How do I pass pickerValue into the main method of the Test class (starting the logic of the main method)? I want num = pickerValue; in the main method (replacing the Scanner). How is this done?
Also, will the print statements I have, System.out.println(...);, translate directly into the Android UI and print on the screen or do I have to modify those?
You should try to separate the UI from the logic. Extract the part where you’re computing something based on inputs from the part where you actually gather the input. That way, the logic methods (or classes) can be reused with several input-gathering methods.
For example, a Calculator class should not have the following methods:
Instead, it should be separated into two classes:
This way, it’s easy to build an AndroidCalculator, because the hard-core math logic is encapsulated in a Calculator class, which doesn’t care about where the inputs come from.