0

I am a beginner programmer and I found a bug / problem in my application that does not give me peace namely I have

W1.setText (numberAsString);
W2.setText (numberAsString);
W3.setText (numberAsString);

in these I have a problem

which each time give a different value of the mathematical result in the textview but sometimes do not update with the variable numberAsString although all the time numberAsString is assigned a different value sometimes given W1, W2, W3 not update

public void Do()
{
    los();
    proba.setText("dodawanie");
    c = Dodawanie(a,b);
    String numberAsString = String.valueOf(c);
    //Toast.makeText(getApplicationContext(), numberAsString, Toast.LENGTH_LONG).show();
    //W1.setText(numberAsString);
    Random liczba = new Random();
    h = liczba.nextInt(5);
    String H = String.valueOf(h);
    String L1 = String.valueOf(l1);
    String L2 = String.valueOf(l2);
    //Toast.makeText(getApplicationContext(), H, Toast.LENGTH_LONG).show();
    Toast.makeText(getApplicationContext(), numberAsString, Toast.LENGTH_LONG).show();
    //////////////////////////////////////////////////////////////////////

    ///////////////////////////////////////////
        if(h==0||h==3) {
            W1.setText(numberAsString);
            Wyb1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    p++;
                    String L = String.valueOf(p);
                    l.setText(L);
                    Do();

                }
            });
            W2.setText(L1);
            Wyb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
            W3.setText(L2);
            Wyb3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
        }
        else if(h==1||h==4){
            W2.setText(numberAsString);
            Wyb2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    p++;
                    String L = String.valueOf(p);
                    l.setText(L);
                    Do();

                }
            });
            W1.setText(L1);
            Wyb1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
            W3.setText(L2);
            Wyb3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                }
            });
        if(h==2||h==5)
            {
                W3.setText(numberAsString);
                Wyb3.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        p++;
                        String L = String.valueOf(p);
                        l.setText(L);
                        Do();

                    }
                });
                W1.setText(L1);
                Wyb1.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                    }
                });
                W2.setText(L2);
                Wyb2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getApplicationContext(), "Zle", Toast.LENGTH_LONG).show();
                    }
                });
            }

    }
}
Maryam Mirzaie
  • 536
  • 6
  • 24
Lamps
  • 25
  • 1
  • 5
  • I suggest you to add `log` so you can see what actual results you are getting from various variables, Please read more about logs [here](https://developer.android.com/studio/debug/am-logcat) – Rahul Gaur Feb 24 '20 at 09:48

1 Answers1

0

You need to be on UI-thread to be able to change TextView content.

Refer: Android update TextView in Thread and Runnable

Nitsshukla
  • 27
  • 1
  • 7