按一下按钮即可进行循环浏览

时间:2019-04-18 09:24:59

标签: java android for-loop

我正在尝试在我的Android应用中创建一项活动,该活动逐项运行先前创建的锻炼运动。此活动基本上是从意图中进行锻炼的,从数据库中创建锻炼项目的ArrayList,然后遍历ArrayList。

问题出在runInReps和runInTime方法上。 runInTime应该获得练习的持续时间(有效),并创建一个倒计时计时器,用于对持续时间进行倒计时。完成后,它应该显示“完成”,并在单击nextRep按钮时移至另一组(或者如果没有剩余,则进行下一个练习-在run方法中完成)。那是行不通的-while循环变为无限,无法退出它。而且计时器甚至永远不会启动。

runInReps应该执行类似的操作,只是没有计时器-完成一组后,您应该单击按钮并移至下一组。一旦没有剩余的集,您应该转到下一个练习(在run方法中完成)。

您能指出我正确的方向吗? 谢谢!

package android.tomdoischer.workOutDroid;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class runWorkout extends AppCompatActivity {
    String workoutName;
    int setsRemaining;
    DatabaseHelperExercise db;

    TextView runNameOfExercise;
    TextView timeOrRepsRemaining;
    TextView exerciseRemaining;
    TextView reps;
    Button nextRep;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run_workout);


        workoutName = getIntent().getStringExtra("WORKOUT_NAME");

        runNameOfExercise = findViewById(R.id.runNameOfExercise);
        timeOrRepsRemaining = findViewById(R.id.timeOrRepsRemaining);
        exerciseRemaining = findViewById(R.id.exerciseRemaining);
        nextRep = findViewById(R.id.nextRep);
        reps = findViewById(R.id.reps);

        System.out.println("reached ifs");

        if(workoutName != null) {
            System.out.println("reached run");
            run();
        } else {
            Toast.makeText(this, "You have not selected a workout!", Toast.LENGTH_LONG).show();
        }
    }

    public void run() {
        // a method which reads individual workoutItems from the database and returns an ArrayList
        // of workoutItems
        ArrayList<WorkoutItem> workoutItems = createRunnableWorkout(workoutName);

        System.out.println("array created");
        // length of the workout
        int length = workoutItems.size();
        int count = 0;


        // running the whole arraylist item by item
        for (WorkoutItem item : workoutItems) {
            System.out.println("running item");
            if (item.getExercise().isMeasureTime()) {
                count++;
                exerciseRemaining.setText(String.format("%s / %s", String.valueOf(count), String.valueOf(length)));
                runInTime(item);
            } else {
                count++;
                exerciseRemaining.setText(String.format("%s / %s", String.valueOf(count), String.valueOf(length)));
                runInReps(item);
            }
        }

        // jumps to the finished workout activity, saves the workout name
        Intent newIntent = new Intent(runWorkout.this, runWorkoutDone.class);
        newIntent.putExtra("WORKOUT_NAME", workoutName);
        startActivity(newIntent);
    }

    public ArrayList<WorkoutItem> createRunnableWorkout(String workoutName) {
        ArrayList<WorkoutItem> workoutItems = new ArrayList<>();

        db = new DatabaseHelperExercise(this);

        Cursor data = db.getDataWorkoutItems(workoutName);

        if (data.getCount() == 0) {
            Toast.makeText(this, "This workout is empty!", Toast.LENGTH_LONG).show();
        } else {
            while (data.moveToNext()) {
                WorkoutItem workoutItem = new WorkoutItem();

                boolean inTime;
                if(data.getInt(5) == 1) {
                    inTime = true;
                } else {
                    inTime = false;
                }

                workoutItem.setExercise(new Exercise(data.getString(2),inTime));
                workoutItem.setSets(data.getInt(3));

                if (workoutItem.getExercise().isMeasureTime()) {
                    workoutItem.setTime(data.getInt(4));
                } else {
                    workoutItem.setReps(data.getInt(4));
                }

                workoutItems.add(workoutItem);
            }
        }

        return workoutItems;
    }

    public void runInTime(WorkoutItem item) { //todo fix me
        // next button not pressed yet
        System.out.println("running runintime");
        setsRemaining = item.getSets();
        System.out.println("setsremaining " + setsRemaining);

        // set text fields
        runNameOfExercise.setText(item.getExercise().getName());
        reps.setText(String.valueOf(item.getReps()));
        timeOrRepsRemaining.setText(String.valueOf(setsRemaining));
        System.out.println("text set");

        nextRep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setsRemaining--;
            }
        });
        System.out.println("onclicklistener set");


        while (setsRemaining > 0) {
            System.out.println("in the while loop");
            CountDownTimer cTimer = new CountDownTimer(item.getTime() * 1000, 1000) {
                public void onTick(long millisUntilFinished) {
                    System.out.println("ontick");
                    timeOrRepsRemaining.setText(String.format("%s s", Long.toString(millisUntilFinished / 1000)));
                    System.out.println("ontick finished");
                }

                public void onFinish() {
                    System.out.println("finished");
                    timeOrRepsRemaining.setText("Done!");
                }
            };
            cTimer.start();
            cTimer.cancel();
        }
    }

    public void runInReps(WorkoutItem item) { //todo fix me
        // next button not pressed yet
        System.out.println("running runinreps");
        setsRemaining = item.getSets();

        // set text fields
        runNameOfExercise.setText(item.getExercise().getName());
        reps.setText(String.format("%s reps", String.valueOf(item.getReps())));

        nextRep.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setsRemaining--;
            }
        });

        while (setsRemaining >0) {
            timeOrRepsRemaining.setText(String.format("%s sets", String.valueOf(setsRemaining)));
        }
    }
}

1 个答案:

答案 0 :(得分:0)

因此,如果有人遇到相同的问题,则问题实际上出在试图以for或while循环遍历ArraýList。实际上,这不是我想要实现的目标-我想在按钮单击时进入ArrayList。

有关更多详细信息,请参见固定代码:

package android.tomdoischer.workOutDroid;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class runWorkout extends AppCompatActivity {
    String workoutName;
    int setsRemaining;
    boolean inTime;
    int move;
    int length;
    int count;
    DatabaseHelperExercise db;
    ArrayList<WorkoutItem> workoutItems;

    TextView runNameOfExercise;
    TextView timeOrRepsRemaining;
    TextView exerciseRemaining;
    TextView reps;
    Button nextRep;
    Button nextEx;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_run_workout);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); // keeps the screen on

        workoutName = getIntent().getStringExtra("WORKOUT_NAME");

        runNameOfExercise = findViewById(R.id.runNameOfExercise);
        timeOrRepsRemaining = findViewById(R.id.timeOrRepsRemaining);
        exerciseRemaining = findViewById(R.id.exerciseRemaining);
        nextRep = findViewById(R.id.nextRep);
        nextEx = findViewById(R.id.nextEx);
        reps = findViewById(R.id.reps);

        System.out.println("reached ifs");

        if(workoutName != null) {
            System.out.println("reached run");
            run();
        } else {
            Toast.makeText(this, "You have not selected a workout!", Toast.LENGTH_LONG).show();
        }
    }

    public void run() {
        // a method which reads individual workoutItems from the database and returns an ArrayList
        // of workoutItems
        workoutItems = createRunnableWorkout(workoutName);

        System.out.println("array created");
        // length of the workout
        length = workoutItems.size();
        move = 0;

        setUpTextViews(move);
    }

    public ArrayList<WorkoutItem> createRunnableWorkout(String workoutName) {
        ArrayList<WorkoutItem> workoutItems = new ArrayList<>();

        db = new DatabaseHelperExercise(this);

        Cursor data = db.getDataWorkoutItems(workoutName);

        if (data.getCount() == 0) {
            Toast.makeText(this, "This workout is empty!", Toast.LENGTH_LONG).show();
        } else {
            while (data.moveToNext()) {
                WorkoutItem workoutItem = new WorkoutItem();

                boolean inTime;
                if(data.getInt(5) == 1) {
                    inTime = true;
                } else {
                    inTime = false;
                }

                workoutItem.setExercise(new Exercise(data.getString(2),inTime));
                workoutItem.setSets(data.getInt(3));

                if (workoutItem.getExercise().isMeasureTime()) {
                    workoutItem.setTime(data.getInt(4));
                } else {
                    workoutItem.setReps(data.getInt(4));
                }

                workoutItems.add(workoutItem);
            }
        }

        return workoutItems;
    }


    public void onNextEx(View v) {
        move++;
        if(move<length) {
            setUpTextViews(move);
        } else {
            Intent newIntent = new Intent(runWorkout.this, runWorkoutDone.class);
            newIntent.putExtra("WORKOUT_NAME", workoutName);
            startActivity(newIntent);
        }
    }

    public void onNextSet(View v) {
        if (inTime) {

        } else {
            if(setsRemaining > 1) {
                setsRemaining--;
                timeOrRepsRemaining.setText(String.format("%s set(s)", String.valueOf(setsRemaining)));
            } else {
                timeOrRepsRemaining.setText("Done!");
            }
        }
    }

    public void setUpTextViews(int move) {
        runNameOfExercise.setText(workoutItems.get(move).getExercise().getName());

        setsRemaining = workoutItems.get(move).getSets();
        inTime = workoutItems.get(move).getExercise().isMeasureTime();

        if(workoutItems.get(move).getExercise().isMeasureTime()) {
            reps.setText(String.valueOf(setsRemaining) + " set(s)");
            new CountDownTimer(workoutItems.get(move).time * 1000, 1000) {
                @Override
                public void onTick(long millisUntilFinished) {
                    timeOrRepsRemaining.setText(millisUntilFinished / 1000 + " s");
                }

                @Override
                public void onFinish() {
                    timeOrRepsRemaining.setText("Done!");
                }
            }.start();


        } else {
            reps.setText(String.valueOf(workoutItems.get(move).getSets()) + " rep(s)");
            timeOrRepsRemaining.setText(String.valueOf(workoutItems.get(move).getReps()) + " set(s)");
        }
        exerciseRemaining.setText((move + 1) + " / " + length);
    }
}