如何从 Java class 中获取结果以显示在 Android activity 中?
How do I get the result from a Java class to display in an Android activity?
我有一个 Java class,它使用 PSO 计算用户在 [=40= 的主要 activity 中输入的优化服务的最佳全局解决方案].当我 运行 我的 UI 时,我已经使用 System.out.println()
在终端中打印了位组合计算,但是我需要在 activity 的 EditText 视图中打印相同的打印称为 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 class (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 视图中打印 System.out.print(b + " ");
。 bestCombo[]
中的布尔变量的数量会根据用户在 main 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
}
我相信您正在寻找的是TextChangedListener
。将第一个 EditText
设置为 addTextChangedListener
,并根据第一个的输入更改第二个 EditText
。
代码示例:
userGlobal.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int aft ) {
}
@Override
public void afterTextChanged(Editable s){
//call your function here for calculation
best.setText(yourfunctioname(userGlobal.getText()));
}
});
您可以在 CustomUseCase.java 中使用一个方法来计算 "bestCombo" 和 returns 它作为字符串而不是将其打印到控制台。类似于:
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 class,它使用 PSO 计算用户在 [=40= 的主要 activity 中输入的优化服务的最佳全局解决方案].当我 运行 我的 UI 时,我已经使用 System.out.println()
在终端中打印了位组合计算,但是我需要在 activity 的 EditText 视图中打印相同的打印称为 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 class (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 视图中打印 System.out.print(b + " ");
。 bestCombo[]
中的布尔变量的数量会根据用户在 main 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
}
我相信您正在寻找的是TextChangedListener
。将第一个 EditText
设置为 addTextChangedListener
,并根据第一个的输入更改第二个 EditText
。
代码示例:
userGlobal.addTextChangedListener(new TextWatcher(){
@Override
public void onTextChanged(CharSequence s, int start, int before, int count){
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int aft ) {
}
@Override
public void afterTextChanged(Editable s){
//call your function here for calculation
best.setText(yourfunctioname(userGlobal.getText()));
}
});
您可以在 CustomUseCase.java 中使用一个方法来计算 "bestCombo" 和 returns 它作为字符串而不是将其打印到控制台。类似于:
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);