Получить данные из Firebase и перейти к следующему действию на экране Splach - PullRequest
0 голосов
/ 23 апреля 2020

У меня есть операция spla sh, которая выполняется в течение 7 се c. Мне нужно извлечь данные из базы Firebase и сохранить их в связке и передать их к следующему действию. Я написал приведенный ниже код, но он не помещает никаких значений в пакет. Пожалуйста, помогите мне с этим. Я хочу, например, во время загрузки экрана spla sh, я хочу получить данные из firebase и перейти к следующему действию. Где я иду не так?

[Я получаю данные из пакета, как на картинке. но, как вы видите, отладчик не показывает доступных переменных]



public class SplashActivity extends AppCompatActivity {

    private FirebaseAuth mAuth;
    private DatabaseReference UsersRef;
    private String currentUserID;
    private Bundle bundle = new Bundle();

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

        initviews();
        checkPermissions();
        new Handler().postDelayed(new Runnable() {


            @Override
            public void run() {
                // This method will be executed once the timer is over
                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                i.putExtras(bundle);
                startActivity(i);
                finish();
            }
        }, 7000);
        UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot)
            {
                if(dataSnapshot.exists())
                {
                    if(dataSnapshot.hasChild("fullname") && dataSnapshot.hasChild("bloodgroup") && dataSnapshot.hasChild("profileimage"))
                    {
                        String fullname = dataSnapshot.child("fullname").getValue().toString();
                        String bloodgroup = dataSnapshot.child("bloodgroup").getValue().toString();
                        String image = dataSnapshot.child("profileimage").getValue().toString();
                        String currentloc = dataSnapshot.child("currentlocation").getValue().toString();
                        String perloc = dataSnapshot.child("permanentlocation").getValue().toString();
                        String mobile = dataSnapshot.child("mobile").getValue().toString();
                        String times = dataSnapshot.child("timesdonated").getValue().toString();
                        String last = dataSnapshot.child("lastdonatedon").getValue().toString();
                        String uid = dataSnapshot.child("uid").getValue().toString();

                        passData(fullname,bloodgroup,image,currentloc,perloc,mobile,times,last,uid);

                    }
                    else
                    {
                        Toast.makeText(SplashActivity.this, "Login or Sign up first", Toast.LENGTH_SHORT).show();
                        Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);
                        finish();
                    }
                }
                else
                {
                    Toast.makeText(SplashActivity.this, "Login or Sign up first", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    finish();
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

    private void passData(String fullname, String bloodgroup, String image, String currentloc, String perloc, String mobile, String times, String last, String uid) {

    bundle.putString("name",fullname);
    bundle.putString("bg",bloodgroup);
    bundle.putString("image",image);
    bundle.putString("curl",currentloc);
    bundle.putString("perl", perloc);
    bundle.putString("mob",mobile);
    bundle.putString("uid",uid);
    bundle.putString("times",times);
    bundle.putString("last",last);
    }
    }
}

1 Ответ

0 голосов
/ 23 апреля 2020

Ваш метод passData помещает значения в bundle, но затем не запускает следующее действие. Весь трюк с отложенным фоновым обработчиком в лучшем случае не нужен, но, вероятно, вызывает больше проблем.

Ваш код должен выглядеть примерно так:

UsersRef.child(currentUserID).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot)
    {
        if(dataSnapshot.exists())
        {
            if(dataSnapshot.hasChild("fullname") && dataSnapshot.hasChild("bloodgroup") && dataSnapshot.hasChild("profileimage"))
            {
                String fullname = dataSnapshot.child("fullname").getValue().toString();
                String bloodgroup = dataSnapshot.child("bloodgroup").getValue().toString();
                String image = dataSnapshot.child("profileimage").getValue().toString();
                String currentloc = dataSnapshot.child("currentlocation").getValue().toString();
                String perloc = dataSnapshot.child("permanentlocation").getValue().toString();
                String mobile = dataSnapshot.child("mobile").getValue().toString();
                String times = dataSnapshot.child("timesdonated").getValue().toString();
                String last = dataSnapshot.child("lastdonatedon").getValue().toString();
                String uid = dataSnapshot.child("uid").getValue().toString();

                passData(fullname,bloodgroup,image,currentloc,perloc,mobile,times,last,uid);

                Intent i = new Intent(SplashActivity.this, MainActivity.class);
                i.putExtras(bundle);
                startActivity(i);
            }
            else
            {
                Toast.makeText(SplashActivity.this, "Login or Sign up first", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(SplashActivity.this, MainActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                startActivity(intent);
                finish();
            }
        }
        else
        {
            Toast.makeText(SplashActivity.this, "Login or Sign up first", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            startActivity(intent);
            finish();
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
...