Vinicius Petrachin
Vinicius Petrachin

Reputation: 329

Android Ui is not updating in thread

In the Log works correctly but in setText does not change anything on the screen, and no error happens.

//within the onCreate method 
myRef.child(fireUser.getUid()).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    millis = Long.valueOf(dataSnapshot.child("millis").getValue().toString());
                    t1.run();
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                    Intent i = new Intent(getApplicationContext(), SplashActivity.class);
                    startActivity(i);
                    finish();
                }
            });

//Out of the onCreate method
    Thread t1 = new Thread(){
            @Override
            public void run() {
                try {
                    long i;
                    for(i=System.currentTimeMillis(); i < millis+millis_wait; i=System.currentTimeMillis()) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                long pr = (millis + millis_wait) - System.currentTimeMillis();
                                tx_time.setText(""+pr);
                                Log.i("TST", "" + pr);
                            }
                        });
                        Thread.sleep(1000);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };

What should I change?

As I said, the code is working, the Log event is being called and executed perfectly, the only thing that does not happen is to update setText simply does not run, but also not from any errors.

Upvotes: 1

Views: 45

Answers (1)

Yunus Kulyyev
Yunus Kulyyev

Reputation: 1022

Call t1.start(); in your UI thread to run it

Upvotes: 2

Related Questions