javaandroidandroid-edittextparticle-swarm

How do I get the result from a Java class to display in an Android activity?


I have a Java class that uses PSO to calculate the best global solution for the optimisation of services that a user inputs in the main activity of a UI. When I run my UI, I have printed the bit combination calculation in the terminal using System.out.println(), however I need that same print to print in my EditText view in an activity called SolutionActivity.java.

Here is what my terminal shows me:

I/System.out: Particle value: 11.0
I/System.out: Particle bit string: [true, false, true, true]
I/System.out: Particle goodness: 5.0
I/System.out: Time spend: 2.8145
I/System.out: Iterations: 4.6209
I/System.out: Success: 3724.0
I/System.out: true false true true 

Here is the code in the Java class (CustomUseCase.java) that prints this...

// ...

    this.found += (bpso.getFound() ? 1 : 0);
    this.iterations += bpso.getSolIterations(); //use the method in bpso to get number of iterations taken
    long end = System.currentTimeMillis() - start; //end time minus start time

    this.sumTimes += end; //override the time spent variable

    System.out.println("Particle value: "      + Particle.getValue(Particle.bestGlobal()));
    System.out.println("Particle bit string: " + Arrays.toString(Particle.bestGlobal()));
    System.out.println("Particle goodness: "   + customService.getGoodness(Particle.bestGlobal()));
}

System.out.println("Time spend: " + sumTimes/max);
System.out.println("Iterations: " + iterations/max);
System.out.println("Success: " + found);

boolean[] bestCombo = Particle.bestGlobal();

for(Boolean b: bestCombo){
    System.out.print(b + " ");
}

System.out.println();

Now I need the System.out.print(b + " "); printed in a EditText view in my SolutionActivity. The number boolean variables in bestCombo[] will vary depending on what the user inputs in the main activity, if the user inputs 3 services, then the bestCombo will have 5 elements, if the user inputs 6 services, the bestCombo will have 8 elements. Here is what it looks like at the moment...

public void setUserResults(){
    EditText userGlobal = (EditText) findViewById(R.id.userGlobal);
    EditText best = (EditText) findViewById(R.id.best);

    best.setText(); //i need the bestCombo solution to be input here!! e.g True False True False
}

Solution

  • You could have a method in CustomUseCase.java that calculates "bestCombo" and returns it as a String rather than printing it to the console. Something like:

    class CustomUseCase {
        .
        .
        .
    
        public static String getBestCombo() {
            .
            .
            .
    
            boolean[] bestCombo = Particle.bestGlobal();
    
            String bestComboString = "";
            for (Boolean b : bestCombo){
                bestComboString = bestComboString + b + " ";
            }
    
            return bestComboString;
        }
    }
    

    And then call it:

    String bestComboString = CustomUseCase.getBestComboString();
    best.setText(bestComboString);