gpt4 book ai didi

java - 如何从 Java 类获取结果以显示在 Android Activity 中?

转载 作者:行者123 更新时间:2023-11-29 04:31:31 27 4
gpt4 key购买 nike

我有一个 Java 类,它使用 PSO 来计算优化用户在 UI 的主要 Activity 中输入的服务的最佳全局解决方案。当我运行我的 UI 时,我已经使用 System.out.println() 在终端中打印了位组合计算,但是我需要在名为 SolutionActivity.java

这是我的终端显示的内容:

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

这是打印此内容的 Java 类 (CustomUseCase.java) 中的代码...

// ...

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();

现在我需要在 SolutionActivity 的 EditText View 中打印 System.out.print(b + "");bestCombo[] 中的 boolean 变量数将根据用户在主 Activity 中输入的内容而有所不同,如果用户输入 3 项服务,则 bestCombo 将包含 5 个元素,如果用户输入 6 项服务, bestCombo 将有 8 个元素。这是它现在的样子...

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
}

最佳答案

您可以在 CustomUseCase.java 中使用一个方法来计算“bestCombo”并将其作为字符串返回,而不是将其打印到控制台。像这样的东西:

class CustomUseCase {
.
.
.

public static String getBestCombo() {
.
.
.

boolean[] bestCombo = Particle.bestGlobal();

String bestComboString = "";
for (Boolean b : bestCombo){
bestComboString = bestComboString + b + " ";
}

return bestComboString;
}
}

然后调用它:

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

关于java - 如何从 Java 类获取结果以显示在 Android Activity 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43636983/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com