1

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

这是我的终端向我展示的内容:

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 + " ");在我的SolutionActivity. 布尔变量的数量bestCombo[]将根据用户在主活动中输入的内容而有所不同,如果用户输入 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
}
4

2 回答 2

2

您可以在 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);
于 2017-04-26T14:50:56.763 回答
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()));
    }
});
于 2017-04-26T14:46:10.237 回答