0
Hello!
I’m having second thoughts about a process I’m having trouble doing. I have the following screen, and I wanted the user Edittext is already filled with the user’s name or the user’s email. I’m new to mobile programming, and I tried to do this with sharedpreferences, but I couldn’t build the code.
Follow the screen of Activity and the firebase bank I’m using.
Follow my Activity code to here.
public class Mensagemactivity extends Appcompatactivity {
EditText edtNome, edtMensagem;
Button btnEnviarMensagem;
Mensagem mensagem;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mensagem);
edtNome = (EditText) findViewById(R.id.edtUsuario);
edtMensagem = (EditText) findViewById(R.id.edtMensagem);
btnEnviarMensagem = (Button) findViewById(R.id.btnEnviarMensagem);
databaseReference = FirebaseDatabase.getInstance().getReference();
btnEnviarMensagem.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mensagem = new Mensagem();
mensagem.setNome(edtNome.getText().toString());
mensagem.setMensagem(edtMensagem.getText().toString());
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("mensagens").push().setValue(firebaseDatabase, new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
//Problem with saving the data
if (databaseError != null) {
Toast.makeText(MensagemActivity.this, "Erro ao criar ocorrência / erro ao inserir dados!", Toast.LENGTH_LONG).show();
} else {
//Data uploaded successfully on the server
databaseReference.child("nome").setValue(edtNome.getText().toString());
databaseReference.child("texto").setValue(edtMensagem.getText().toString());
Toast.makeText(MensagemActivity.this, "Mensagem enviada com sucesso!", Toast.LENGTH_LONG).show();
retornaTela();
}
}
});
}
});
}
If anyone would be so kind as to help me, I’d appreciate it.
Hugs.
All right, Eduardo, thanks for the answer. But, this code of yours, is it inserted in on create? In case I used it as a basis, I would include it in that place in my posted above (of course with the necessary amendments)?
– D.Almeida
This, it is created in the oncreate method, firebase.addeventlistener is responsible for activating it, but you can enter firebase.addeventlistener in the on Start method, so every time Activity is launched it will call Eventoouvinte, also advise to overwrite the onStop method and insert the following (for resource saving): firebase.removeEventListener(valueEventListener);
– Eduardo Rafael Moraes
Eduardo, this Sharedpreferencesuser, you created how? Because I couldn’t implement sharedpreference with these same parameters that you set.
– D.Almeida