简体   繁体   中英

Android EditText to int in inner class

So I'm getting one tricky problem when trying to decrement value of EditText. Let me explain, I have some CountDownTimers and when second timer gets to the end a value that's setted before needs to be decremented so the timers won't repeat infinite amount of times.

Here's my code:

public void poslijeDelaya() {

                int counterInt = Integer.parseInt(editTextNOI.getText().toString());

                if (counterInt > 0) {


                    String tekstTrci = editTextHIIVrijeme.getText().toString();
                    long longTrci = Long.parseLong(tekstTrci);

                    CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            updateTimer((int) millisUntilFinished / 1000);

                        }

                        @Override
                        public void onFinish() {
                            textViewGlavniTimer.setText("00:00");
                            textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.liiState));

                            String tekstHodaj = editTextLIIVrijeme.getText().toString();
                            long longHodaj = Long.parseLong(tekstHodaj);

                            CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    updateTimer((int) millisUntilFinished / 1000);
                                }

                                @Override
                                public void onFinish() {
                                    counterInt --;
                                    textViewGlavniTimer.setText("00:00");
                                    textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.hiiState));
                                    poslijeDelaya();

                                }
                            }.start();

                        }
                    }.start();
                } else {
                    textViewNatpisZaOpisAktivnosti.setText("Done");
                }
            }
        });

The tricky part is on counterInt --; part. It says that the variable needs to be declared as final because it's accessed from inner class so I set int counterInt = Integer.parseInt(editTextNOI.getText().toString()); to final int counterInt = Integer.parseInt(editTextNOI.getText().toString()); and guess what?

It says that it can't assign value to a final variable.

Any ideas on how to make Android Studio to stop trolling me?

Make counterInt as a member variable and you are good to go.

private int counterInt;

Assign the value whenever you want.

Also, move counterInt = Integer.parseInt(editTextNOI.getText().toString()); outside poslijeDelaya() .

You can make your int a class variable, but then you could run in to problems where the value is accesses from more than one thread. Instead, make the int class level in the CountDownTimer class. Like this:

 CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {

                        int counterInt = Integer.parseInt(editTextNOI.getText().toString()); <---- add your variable here

                        @Override
                        public void onTick(long millisUntilFinished) {
                            ...
                        }

                        ....
                        CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                            @Override
                            public void onTick(long millisUntilFinished) {
                                updateTimer((int) millisUntilFinished / 1000);
                            }

                            @Override
                            public void onFinish() {
                                counterInt --;  <-- now this is accessible  
                                ....
                            }
                        }.start();
                        }
                    }.start();

当您在超类中添加用于递减counterInt的新方法然后仅在onFinish()中调用此方法时,可能会有所帮助?

After reading your code further, I realize my previous answer wont work because you are calling the method recursively. So instead try this:

public void poslijeDelaya(final int count) {
                if (counterInt > 0) {

                    String tekstTrci = editTextHIIVrijeme.getText().toString();
                    long longTrci = Long.parseLong(tekstTrci);

                    CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            updateTimer((int) millisUntilFinished / 1000);

                        }

                        @Override
                        public void onFinish() {
                            textViewGlavniTimer.setText("00:00");
                            textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.liiState));

                            String tekstHodaj = editTextLIIVrijeme.getText().toString();
                            long longHodaj = Long.parseLong(tekstHodaj);

                            CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    updateTimer((int) millisUntilFinished / 1000);
                                }

                                @Override
                                public void onFinish() {
                                    textViewGlavniTimer.setText("00:00");
                                    textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.hiiState));
                                    poslijeDelaya(counterInt - 1);

                                }
                            }.start();

                        }
                    }.start();
                } else {
                    textViewNatpisZaOpisAktivnosti.setText("Done");
                }
            }
        });

Then, call initially like this:

int counterInt = Integer.parseInt(editTextNOI.getText().toString());
poslijeDelaya(counterInt);

THE SOLUTION:

int counterInt; // as a global variable



counterInt = Integer.parseInt(editTextNOI.getText().toString()); // in onCreate()



 public void poslijeDelaya(final int count) {

                if (counterInt > 0) {


                    String tekstTrci = editTextHIIVrijeme.getText().toString();
                    long longTrci = Long.parseLong(tekstTrci);

                    textViewRundiPreostalo.setText(Integer.toString(counterInt));
                    CountDownTimer timerTrci = new CountDownTimer(longTrci * 1000 + 100, 1000) {
                        @Override
                        public void onTick(long millisUntilFinished) {
                            updateTimer((int) millisUntilFinished / 1000);

                        }

                        @Override
                        public void onFinish() {
                            textViewGlavniTimer.setText("00:00");
                            imageViewSlikaZaOpisAktivnosti.setImageResource(R.drawable.timer_niski_intenzitet);
                            textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.liiState));
                            String tekstHodaj = editTextLIIVrijeme.getText().toString();
                            long longHodaj = Long.parseLong(tekstHodaj);

                            CountDownTimer timerHodaj = new CountDownTimer(longHodaj * 1000 + 100, 1000) {
                                @Override
                                public void onTick(long millisUntilFinished) {
                                    updateTimer((int) millisUntilFinished / 1000);
                                }

                                @Override
                                public void onFinish() {
                                    textViewGlavniTimer.setText("00:00");
                                    textViewNatpisZaOpisAktivnosti.setText(getResources().getString(R.string.hiiState));
                                    counterInt--;
                                    poslijeDelaya(counterInt);
                                }
                            }.start();

                        }
                    }.start();
                } else {
                    textViewNatpisZaOpisAktivnosti.setText("Done");
                }
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM