Doubt with firebase

1

How do I pass this information out of onDataChange into the same activity all within 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!

    
asked by anonymous 18.09.2017 / 15:46

2 answers

0

There are several ways to resolve this problem.

  • Improving architecture
  • You can apply MVP or MVVM on your project and more efficiently modularize your project, making it more weakly coupled, so you can easily reuse your layers of data access.

  • Use 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 Android component, LocalBroadcast to do this, but it ends up getting more code and complicated.

        
    18.09.2017 / 23:59
    0

    I do not 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();
    
        }
    
        
    20.09.2017 / 04:11