如何从Java类中获取结果以显示在Android活动中?

时间:2017-04-26 14:24:10

标签: java android android-edittext particle-swarm

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

以下是我的终端显示的内容:

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

现在我需要在System.out.print(b + " ");的EditText视图中打印SolutionActivitybestCombo[]中的布尔变量数取决于用户在主活动中输入的内容,如果用户输入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
}

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:1)

我相信你所寻找的是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())); } });

代码示例:

$('#link_to_id').find('option').not(':selected').each(function(k,v){
    console.log(k,v.text, v.value);
});