Return value in string with firebase

0

In Android Studio I have the following code in MainActivity

package br.alan.com.firebaseapp;

import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ButtonBarLayout;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

public class MainActivity extends AppCompatActivity {
    //Referencia a banco de dados do site do firebase
    private DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
    //Referencia o nome do banco de dados
    public DatabaseReference usuarioReferencia = databaseReference;
    private TextView texto;
    private Button botaoValor;
    private EditText valor;


    @Override
    public View findViewById(@IdRes int id) {
        return super.findViewById(id);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        texto = (TextView) findViewById(R.id.textView2);
        botaoValor = (Button) findViewById(R.id.botaoId);
        valor = (EditText) findViewById(R.id.valorId);



        botaoValor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String valorDigitado = valor.getText().toString();

                usuarioReferencia.child(valorDigitado).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        if(dataSnapshot.exists()) {
                            texto.setText(dataSnapshot.getValue().toString());
                        }else{
                            texto.setText("Não existe!");
                        }
                    }
                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        texto.setText("Dados não encontrado!");
                    }
                });
            }
        });

    }
}

In firabase I have the values

WhenIruntheAndroidStudioemulatorIhavethefollowingdata:

As I do to format this string, take this {} which looks like it's returning a json. When I put texto.setText(dataSnapshot.getValue().toString()); in code it is returning that data. I would like to know how to return without {} and know how to manipulate this data.

    
asked by anonymous 31.08.2017 / 19:43

1 answer

2

I think the best way for a future adaptation of the code would be to create a class that instantiates this data, in this case a class called User :

public class User {

    public String name;
    public String email;

    public User() {
    
    }

    public User(String name, String email) {
        this.name= name;
        this.email = email;
    }

    public String getName(){
        return this.name;
    }

}

Here the code returns the class you want:

ValueEventListener postListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    
    }
};

Here you already have a user instance with the available data, so just work with this data:

user.getName(); //Retorna o nome
    
31.08.2017 / 20:52