Doubt with the firebase

Asked

Viewed 58 times

1

How do I get this information out of the onDataChange in the same activity everything within the onCreate....

        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

        String teste = (String) (dataSnapshot.child("texto").getValue());
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    }); 

Thank you in advance!

  • 1

    I have that doubt too!!

  • I’m having to do a lot of code because of this, type to using badge in my menu Drawer, only I can not pass the result in the oncreate of the main menu, then I have to make a code in the login and go through putExtra and pass on the same information to adjacent activities to always keep Drawer informed, it sucks :)

2 answers

0

I don’t know if it’s the best way but it works.

private String mResultadoTeste = null;

private Button mBtnTeste;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agendamento);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

        mBtnTeste = (Button) findViewById(R.id.nome);

        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {


        mResultadoTeste = (String) (dataSnapshot.child("texto").getValue());

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    }); 


        mBtnTeste.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {


                metodo2();

            }
        });

}

    //Fora do OnCreate
    metodo2(){


        Toast.makeText(AgendamentoActivity.this, "Mensagem" +mResultadoTeste, Toast.LENGTH_LONG).show();

    }
  • I need it done on oncreate friend.

0

There are many ways to solve this problem.

  1. Improving the architecture

You can apply MVP or MVVM in your project and modularize your project more efficiently by making it weakly coupled, with this you can easily reuse your data access layers.

  1. Utilise Eventbus or other similar libraries

This makes it easy to communicate between Activities, Fragments, Threads, Services, etc. You also have the option to use a native component of Android, the LocalBroadcast to do this, but ends up getting more code and complicated.

  • OK, I’ll take a look at these tips ,thank you for the answer.

  • hello, so I looked at these tips, entedi +/- how it works... more I could not implement with firebase... you would know how to do?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.