如何在ASyncTask中访问数组索引以进行UI更新?

时间:2022-04-03 09:02:31

I have an ASyncTask which is running the update for my UserInterface; but I am not sure how I can access my AnalogInput[] parameter and check which index I am currently on for when I call my publishProgress();

我有一个ASyncTask,它正在运行UserInterface的更新;但是我不知道如何在调用我的publishProgress()时访问我的AnalogInput []参数并检查我当前使用的索引;

private class AnalogUpdater extends AsyncTask<AnalogInput[], Float, Void> {

    // A callback method executed on non UI thread, invoked after
    // onPreExecute method if exists

    // Takes a set of parameters of the type defined in your class
    // implementation. This method will be
    // executed on the background thread, so it must not attempt to interact
    // with UI objects.
    @Override
    protected Void doInBackground(AnalogInput[]... params) {

        try {
            for (int i = 0; i < 15; i++) {
                publishProgress(params[0][i].getVoltage());
            }

        } catch (ConnectionLostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    protected void onProgressUpdate(Float... f) {
        // Getting reference to the TextView tv_counter of the layout
        // activity_main
        final String str = String.format("Voltage: %.2f", f);
        final int v = Math.round(f[0] * 100);
        switch (idx // How can I get this index?) {
        case 1:
            analogInput0VoltageText.setText(str);
            analogInput0VoltageBar.setProgress(v);
            break;
        case 2:
            analogInput1VoltageText.setText(str);
            analogInput1VoltageBar.setProgress(v);
            break;
        case 3:
            analogInput2VoltageText.setText(str);
            analogInput2VoltageBar.setProgress(v);
            break;
        case 4:
            analogInput3VoltageText.setText(str);
            analogInput3VoltageBar.setProgress(v);
            break;
        case 5:
            analogInput4VoltageText.setText(str);
            analogInput4VoltageBar.setProgress(v);
            break;
        case 6:
            analogInput5VoltageText.setText(str);
            analogInput5VoltageBar.setProgress(v);
            break;
        case 7:
            analogInput6VoltageText.setText(str);
            analogInput6VoltageBar.setProgress(v);
            break;
        case 8:
            analogInput7VoltageText.setText(str);
            analogInput7VoltageBar.setProgress(v);
            break;
        case 9:
            analogInput8VoltageText.setText(str);
            analogInput8VoltageBar.setProgress(v);
            break;
        case 10:
            analogInput9VoltageText.setText(str);
            analogInput9VoltageBar.setProgress(v);
            break;
        case 11:
            analogInput10VoltageText.setText(str);
            analogInput10VoltageBar.setProgress(v);
            break;
        case 12:
            analogInput11VoltageText.setText(str);
            analogInput11VoltageBar.setProgress(v);
            break;
        case 13:
            analogInput12VoltageText.setText(str);
            analogInput12VoltageBar.setProgress(v);
            break;
        case 14:
            analogInput13VoltageText.setText(str);
            analogInput13VoltageBar.setProgress(v);
            break;
        case 15:
            analogInput14VoltageText.setText(str);
            analogInput14VoltageBar.setProgress(v);
            break;
        case 16:
            analogInput15VoltageText.setText(str);
            analogInput15VoltageBar.setProgress(v);
            break;
        }

    }

Hopefully this is clear; refer to the comment "How can I get this index?" in the code. }

希望这很清楚;请参阅注释“我如何获得此索引?”在代码中。 }

1 个解决方案

#1


1  

You could use an array as the second parameter on your asyncTask (might not be necessary, I don't really remember)

您可以使用数组作为asyncTask的第二个参数(可能没有必要,我真的不记得了)

private class AnalogUpdater extends AsyncTask<AnalogInput[], Float[], Void>

Pass both values, voltage and index (cast index to float)

传递值,电压和指数(投射指数为浮动)

publishProgress(params[0][i].getVoltage(), (float)i);

And use it later (cast to int again)

稍后再使用它(再次转换为int)

protected void onProgressUpdate(Float... f)
{
    idx = (int)f[1];
}

#1


1  

You could use an array as the second parameter on your asyncTask (might not be necessary, I don't really remember)

您可以使用数组作为asyncTask的第二个参数(可能没有必要,我真的不记得了)

private class AnalogUpdater extends AsyncTask<AnalogInput[], Float[], Void>

Pass both values, voltage and index (cast index to float)

传递值,电压和指数(投射指数为浮动)

publishProgress(params[0][i].getVoltage(), (float)i);

And use it later (cast to int again)

稍后再使用它(再次转换为int)

protected void onProgressUpdate(Float... f)
{
    idx = (int)f[1];
}