如何通过在Java中恢复数据来设置int值

问题描述 投票:0回答:1

我有一个称为initData()的私有void,其中有一些数据(逻辑大声笑),并且在这些数据中有一个等于数字的字符串。我想调出这个数字并将其归因于int或任何我可以用来做

的东西
If anotherint != the number I want to recuperate  
{
  // execute the code
}

而且我不知道该怎么做...

这里是我使用的数据的一个例子list.add(new Exercise(R.drawable.jumpsquats, "Enchaîne un squat avec un saut.", "Squats Sautés", "3"));数字的字符串名称为set_number

我已经用ex_name.setText(list.get(id).getName());所以我做了一个getSet_number但不知道如何使用它

public String getSet_number() {
        return set_number;
    }

    public void setSet_number(String detail) {
        this.set_number = set_number;
    }
}

这里是在@Christian Seiler的帮助下修改的代码

 int set_number = Integer.parseInt(list.get(ex_id).getSet_number());
                        if(actual_set < set_number)
                        {

                            showRestTime();
                            actual_set++;
                        }
                        else {

                            showRestTime();
                            ex_id++;
                            progressBar.setProgress(ex_id);
                            txtTimer.setText("");
                            String s = String.valueOf(actual_set);
                            list.get(ex_id).setSet_number(String.valueOf(0));


                        }

这是我的完整代码:

package com.example.workout_sure;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.example.workout_sure.Database.SWADB;
import com.example.workout_sure.Model.Exercise;
import com.example.workout_sure.Utils.Common;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import me.zhanghai.android.materialprogressbar.MaterialProgressBar;

public class Daily_Training<Foo> extends AppCompatActivity {

    Button btnStart;
    ImageView ex_image;
    TextView txtGetReady,txtCountdown,txtTimer,ex_name,detail_text;
    ProgressBar progressBar;
    LinearLayout layoutGetReady;




    int ex_id=0,limit_time=0,set_number,actual_set=0;

    List<Exercise> list = new ArrayList<>();

    SWADB swaDB;


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

        initData();
        swaDB =  new SWADB(this);





        btnStart = (Button)findViewById(R.id.btnStart);

        ex_image = (ImageView)findViewById(R.id.detail_image);
        detail_text = (TextView)findViewById(R.id.detail_text);

        txtCountdown = (TextView)findViewById(R.id.txtCountdown);
        txtGetReady = (TextView)findViewById(R.id.txtGetReady);
        txtTimer = (TextView)findViewById(R.id.timer);
        ex_name = (TextView)findViewById(R.id.title);

        layoutGetReady = (LinearLayout)findViewById(R.id.layout_get_ready);

        progressBar = (MaterialProgressBar)findViewById(R.id.progressBar);



        //Set data
        progressBar.setMax(list.size());

        setExerciseInformation(ex_id);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(btnStart.getText().toString().toLowerCase().equals("commencer"))
                {
                    showGetReady();
                    btnStart.setText("fini");
                }
                else if(btnStart.getText().toString().toLowerCase().equals("fini"))
                {


                    if(ex_id < list.size())

                    {
                        int set_number = Integer.parseInt(list.get(ex_id).getSet_number());
                        if(actual_set < set_number)
                        {

                            showRestTime();
                            actual_set++;
                        }
                        else {

                            showRestTime();
                            ex_id++;
                            progressBar.setProgress(ex_id);
                            txtTimer.setText("");
                            String s = String.valueOf(actual_set);
                            list.get(ex_id).setSet_number(String.valueOf(0));


                        }
                    }
                    else
                        showFinished();
                }

                else
                {
                    if(swaDB.getSettingMode() == 0)
                        exercisesEasyModeCountDown.cancel();
                    if(swaDB.getSettingMode() == 1)
                        exercisesMediumModeCountDown.cancel();
                    if(swaDB.getSettingMode() == 2)
                        exercisesHardModeCountDown.cancel();
                    restTimeCountDown.cancel();

                    if(ex_id < list.size())
                        setExerciseInformation(ex_id);
                    else
                        showFinished();
                }


            }
        });
    }

    private void showRestTime() {
        ex_image.setVisibility(View.INVISIBLE);
        btnStart.setText("passer");
        btnStart.setVisibility(View.VISIBLE);
        txtTimer.setVisibility(View.INVISIBLE);
        detail_text.setVisibility(View.INVISIBLE);

        layoutGetReady.setVisibility(View.VISIBLE);

        restTimeCountDown.start();

        txtGetReady.setText("TEMPS DE REPOS");


    }

    private void showGetReady() {
        ex_image.setVisibility(View.INVISIBLE);
        btnStart.setVisibility(View.INVISIBLE);
        txtTimer.setVisibility(View.VISIBLE);
        detail_text.setVisibility(View.INVISIBLE);




        layoutGetReady.setVisibility(View.VISIBLE);

        txtGetReady.setText("PREPARE TOI");
        new CountDownTimer(6000,1000)
        {

            @Override
            public void onTick(long millisUntilFinished) {
                txtCountdown.setText(""+(millisUntilFinished/1000));
            }

            @Override
            public void onFinish() {
                    showExercises();


            }
        }.start();

    }

    private void showExercises() {
        if(ex_id < list.size())  //list size y'a ts les exos
        {


                ex_image.setVisibility(View.VISIBLE);
                btnStart.setVisibility(View.VISIBLE);
                layoutGetReady.setVisibility(View.INVISIBLE);
                detail_text.setVisibility(View.VISIBLE);



                    //Set Data youno

                ex_image.setImageResource(list.get(ex_id).getImage_id());
                ex_name.setText(list.get(ex_id).getName());
                detail_text.setText(list.get(ex_id).getDetail());





        }
        else
            showFinished();

    }



    private void showFinished() {

        ex_image.setVisibility(View.INVISIBLE);
        btnStart.setVisibility(View.INVISIBLE);
        txtCountdown.setVisibility(View.INVISIBLE);
        layoutGetReady.setVisibility(View.VISIBLE);
        detail_text.setVisibility(View.INVISIBLE);
        progressBar.setVisibility(View.INVISIBLE);


        txtGetReady.setText("FINISHED !!!");
        txtCountdown.setText("Félicitations ! \n Tu as fini ton entraînement ;) ");
        txtCountdown.setTextSize(20);

        //Sauvegarder le workout de fou tavusa dans la db
        swaDB.saveDay(""+ Calendar.getInstance().getTimeInMillis());
    }

    // Countdown

    CountDownTimer exercisesEasyModeCountDown = new CountDownTimer(Common.TIME_LIMIT_EASY,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            txtTimer.setText(""+1/1000);
        }

        @Override
        public void onFinish() {
            if(ex_id < list.size() -1)
            {
                ex_id++;
                progressBar.setProgress(ex_id);
                txtTimer.setText("");


                setExerciseInformation(ex_id);
                btnStart.setText("Commencer");

            }
            else
            {
                showFinished();
            }
        }
    };
    CountDownTimer exercisesMediumModeCountDown = new CountDownTimer(Common.TIME_LIMIT_EASY,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            txtTimer.setText(""+1/1000);
        }

        @Override
        public void onFinish() {
            if(ex_id < list.size() -1)
            {
                ex_id++;
                progressBar.setProgress(ex_id);
                txtTimer.setText("");


                setExerciseInformation(ex_id);
                btnStart.setText("Commencer");

            }
            else
            {
                showFinished();
            }
        }
    };
    CountDownTimer exercisesHardModeCountDown = new CountDownTimer(Common.TIME_LIMIT_EASY,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            txtTimer.setText(""+1/1000);
        }

        @Override
        public void onFinish() {
            if(ex_id < list.size() -1)
            {
                ex_id++;
                progressBar.setProgress(ex_id);
                txtTimer.setText("");


                setExerciseInformation(ex_id);
                btnStart.setText("Commencer");

            }
            else
            {
                showFinished();
            }
        }
    };


    CountDownTimer restTimeCountDown = new CountDownTimer(10000,1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            txtCountdown.setText(""+(millisUntilFinished/1000));
        }

        @Override
        public void onFinish() {

            setExerciseInformation(ex_id);
            showExercises();
        }
    };


    private void setExerciseInformation(int id) {

        ex_image.setImageResource(list.get(id).getImage_id());
        ex_name.setText(list.get(id).getName());
        detail_text.setText(list.get(id).getDetail());
        btnStart.setText("Commencer");






        ex_image.setVisibility(View.VISIBLE);
        btnStart.setVisibility(View.VISIBLE);
        txtTimer.setVisibility(View.VISIBLE);
        detail_text.setVisibility(View.VISIBLE);


        layoutGetReady.setVisibility(View.INVISIBLE);



    }







    private void initData() {

        list.add(new Exercise(R.drawable.test,"Positionne-toi face au sol, les mains hauteur d’épaules. Les jambes ne sont pas loin derrière. Rapproche tes jambes de manière à former un angle avec ton corps.\n" +
                "Ton dos doit rester bien droit et ta tête doit être dans le même alignement que ton dos pendant toute l’exécution du mouvement.\n" +
                "Descends en PLONGEANT EN AVANT tout en restant le dos droit  puis repousse fermement pour remonter.\n" ," Pompes Pike","3"));
        list.add(new Exercise(R.drawable.clappushups, "Mets toi en position pompe (dos droit, bien gainé, bras tendus), descends jusqu'à ce que tes pecs touchent le sol puis pousse de façon explosive de sorte à faire décoller ton corps loin du sol et pouvoir claquer dans tes mains.", "Pompes claquées", "3"));
        list.add(new Exercise(R.drawable.dips, "En appui sur les barres, buste légèrement penché vers l’avant , fléchis les bras jusqu’à ce que ceux-ci se retrouvent à l’horizontal , puis pousse pour revenir à la position initiale.Reste toujours gainé.", "Dips", "3"));
        list.add(new Exercise(R.drawable.diamondpushups, "Mets tes mains en prise serrée ou en forme de triangle, comme sur la video ci-dessus, fais ton maximum de pompes.\n Sans prendre de repos, écarte tes mains et fais ton maximum de répétitions. \n Enfin, reprends une prise basique avec tes mains et fais ton maximum de pompes.", "Superset Pompes(Diamant-Ecartées-Basiques)", "3"));
        list.add(new Exercise(R.drawable.jumpsquats, "Enchaîne un squat avec un saut.", "Squats Sautés", "3"));

    }
}

非常感谢,斯特凡]]

我有一个称为initData()的私有void,其中有一些数据(逻辑大声笑),并且在这些数据中有一个等于数字的字符串。我想调养这个数字和属性...

java android string mobile int
1个回答
1
投票

如果需要String的int值,则最大使用'Integer.parseInt((set_number)'请注意,如果无法将“ set_number”解析为一个int,则此方法将引发“ NumberFormatException”。

© www.soinside.com 2019 - 2024. All rights reserved.