如何通过自动更改值来更新进度条最大值



我正在使用Google fit API开发一款健身应用程序。

来自 API 的步骤数据被传递到带有参数(3300,"步骤"(stepDisplayer的方法。

我正在尝试value == daily1更新进度条,以便pbStepBar.setMax(daily2)条形的最大值,并在value == daily2时再次pbStepBar.setMax(daily3),而条形的进度始终设置为值,例如 3500/4000 然后是 4102/8000 和 8900/12000。

我尝试使用一个 switch 语句,将目标与值进行比较,该方法名为 setNextGoal(value) 与 getter 和 setter 方法getCurrentGoal()setCurrentGoal(),如下所示,但这不起作用。当我运行应用程序时,最大值不会从daily1发生变化,它看起来像这样的 5050/4000。

我做错了什么?我可以将pbStepBar替换为另一个进度条和切换可见性吗?

布局

<ProgressBar
        android:id="@+id/pbStepBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="300dp"
        android:layout_height="15dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="25dp"
        android:indeterminate="false"
        android:scaleY="3"
        />

public class MainActivity {
//these are goals
int daily1 = 4000
int daily2 = 8000
int daily3 = 12000
//set to this onCreate
pbStepBar.setMax = daily1
private void stepDisplayer(final int value, final String field) {
    tvHomeSteps = (TextView) findViewById(R.id.tvHomeSteps);
    pbStepBar = (ProgressBar) findViewById(R.id.pbStepBar);
    setNextGoal(value);
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            tvHomeSteps.setText("today's " + field + " " + value + "/" + dailyStepGoal);
        }
    });
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (value <= getCurrentGoal()) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        pbStepBar.setProgress(value);
                    }
                });
                try {
                    Thread.sleep(25);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (getCurrentGoal() == value) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(), "Well done you reached your daily goal of " + getCurrentGoal() + " " + field,
                                    Toast.LENGTH_LONG).show();
                        }
                    });
                }
                setNextGoal(value);
            }
        }
    }).start();
}
private void setNextGoal (final int value) {
    switch (value) {
        case dailyStepGoal:
            setCurrentGoal(dailyStepGoal + stepGoalIncrement);
            pbStepBar.setMax(dailyStepGoal + stepGoalIncrement);
            break;
        case dailyStepGoal + stepGoalIncrement:
            setCurrentGoal(dailyStepGoal + (stepGoalIncrement*2));
            pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*2));
            break;
        case dailyStepGoal + (stepGoalIncrement*2):
            setCurrentGoal(dailyStepGoal + (stepGoalIncrement*3));
            pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*3));
            break;
        case dailyStepGoal + (stepGoalIncrement*3):
            setCurrentGoal(dailyStepGoal + (stepGoalIncrement*4));
            pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*4));
            break;
        case dailyStepGoal + (stepGoalIncrement*4):
            setCurrentGoal(dailyStepGoal + (stepGoalIncrement*5));
            pbStepBar.setMax(dailyStepGoal + (stepGoalIncrement*5));
            break;
    }
}

我找到了解决方案。

我使用如下所示的 int 变量currentGoal来跟踪当前目标。我认为问题是我试图从 UI 线程调用活动中的方法。我用了一个简单的if语句,不知道为什么我没有早点想到这个。

我还测试了它运行良好的解决方案。

private void stepDisplayer(final int value, final String field( {

    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            if (value >= currentGoal) {
                Toast.makeText(getApplicationContext(), "Well done you reached your daily goal of " + currentGoal + " " + field,
                        Toast.LENGTH_LONG).show();
                currentGoal += stepGoalIncrement;
                tvHomeSteps.setText("today's " + field + " " + value + "/" + currentGoal);
            }
            else {
                tvHomeSteps.setText("today's " + field + " " + value + "/" + currentGoal);
            }
        }
    });
    new Thread(new Runnable() {
        @Override
        public void run() {
            while (value <= currentGoal) {
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        pbStepBar.setProgress(value);
                        if (value >= pbStepBar.getMax()) {
                            pbStepBar.setMax(currentGoal);
                            pbStepBar.setProgress(value);
                        }
                        else {
                            pbStepBar.setProgress(value);
                        }
                    }
                });
                try {
                    Thread.sleep(25);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
                //setNextGoal(pbStepBar, value);
            }
        }
    }).start();
}

最新更新