0

How do I display the data that signed up in Firebase in my application and make them persist in it, so they can be viewed even offline Would you like some practical examples ...

//Classe dos usuarios

public class Usuarios {

private String Nome;
private String Data;
private String Rg;
private String Cpf;
private String Endereco;
private String Doenca;
private String Profissao;

public Usuarios(String Nome, String Data, String Rg, String Cpf, String Endereco, String Doenca, String Profissao) {
    this.Nome = Nome;
    this.Data = Data;
    this.Rg = Rg;
    this.Cpf = Cpf;
    this.Endereco = Endereco;
    this.Doenca = Doenca;
    this.Profissao = Profissao;
}
public Usuarios() {

}

public String getNome() {
    return Nome;
}

public void setNome(String nome) {
    Nome = nome;
}

public String getData() {
    return Data;
}

public void setData(String data) {
    Data = data;
}

public String getRg() {
    return Rg;
}

public void setRg(String rg) {
    Rg = rg;
}

public String getCpf() {
    return Cpf;
}

public void setCpf(String cpf) {
    Cpf = cpf;
}

public String getEndereco() {
    return Endereco;
}

public void setEndereco(String endereco) {
    Endereco = endereco;
}

public String getDoenca() {
    return Doenca;
}

public void setDoenca(String doenca) {
    Doenca = doenca;
}

public String getProfissao() {
    return Profissao;
}

public void setProfissao(String profissao) {
    Profissao = profissao;
   }
}

//Class Main public class Main extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
private void writeNewUser(String userId,String Nome, String Data, String Rg, String Cpf, String Endereco, String Doenca, String Profissao) {
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference myRef = database.getReference();
    Usuarios user = new Usuarios(Nome, Data, Rg, Cpf, Endereco, Doenca, Profissao);

    myRef.child("users").child(userId).setValue(user);
    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Usuarios user = dataSnapshot.getValue(Usuarios.class);

           String nome = Usuarios.class.getName();

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

//Classe cadastro public void cadastro(View view){ FirebaseDatabase database = FirebaseDatabase.getInstance(); DatabaseReference myRef = database.getReference(); myRef.child("Users").child(etRg.getText().toString()).child("UserName").setValue(etNome.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserData").setValue(etData.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserRg").setValue(etRg.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserCpf").setValue(etCpf.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserEndereco").setValue(etEndereco.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserDoenca").setValue(etDoenca.getText().toString()); myRef.child("Users").child(etRg.getText().toString()).child("UserProfissao").setValue(etProfissao.getText().toString()); } public void mostrarDados(View view){ textViewInfo.setText("UserName"); }

enter image description here

1 Answers1

0

1st of All it is not best practice to save your data, witch way you doing this, make a Java POJO object to save data Into firebase, here is video Tutorial i make to save java Obj to Firebase , here is Example How you Writer java obj to firebase

step one make your java Class

@IgnoreExtraProperties
public class User {

    public String username;
    public String email;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

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

}

step 2

private void writeNewUser(String userId, String name, String email) {
    User user = new User(name, email);

    mDatabase.child("users").child(userId).setValue(user);
}

Then You need add ValueEventListener To read data

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

String Name user.getName();

                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });

For more read The google Doc

Hamza
  • 2,017
  • 1
  • 19
  • 40
  • Man I did as changes in class que fast you sugerio however now she is not no more saving the data ... – Matheus Oliveira Sep 25 '16 at 00:49
  • do you get your data? – Hamza Sep 25 '16 at 05:22
  • no they did not come to the bank, the application and displays an error – Matheus Oliveira Sep 25 '16 at 05:28
  • this is error NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference at com.example.matheus.kypy.Main.cadastro(Main.java:76) – Matheus Oliveira Sep 25 '16 at 05:40
  • delete this line and try this myRef.child("users").child(userId).setValue(user);http://stackoverflow.com/a/32901133/5727285 – Hamza Sep 25 '16 at 05:50
  • I Can not entender What the boy makes no link code ... I never imagined it would be so hard to save data and display them using a api :( – Matheus Oliveira Sep 25 '16 at 06:07
  • its not hard share your code on git hub i will check what doing wrong – Hamza Sep 25 '16 at 06:16
  • this how i done ,https://github.com/ameerhamza6733/FirebaseReadAndWriteDemo/tree/master/app/src/main/java/com/example/ameerhamza/firebabseyoutubetest – Hamza Sep 25 '16 at 06:22
  • ahhh I do not know What do u already I have a bursting head, I I used your code to guthub and IT tb NOT work here, HE monstra In records What IS RUNNING is everything BUT no console do firebase IT NOT displays nd, OR brings back as information ... I am NOT getting Doing nothing today – Matheus Oliveira Sep 25 '16 at 07:33
  • have you change your fire base rules? – Hamza Sep 25 '16 at 09:28
  • i am gatting your data in my firebase, so make sure you study the logs(log.e("","')) in your android study , here is video link to video tutorial https://www.youtube.com/watch?v=SO1x2rENZMk – Hamza Sep 25 '16 at 09:36
  • guy thank your video lessons helped me a lot ... I did everything according to their videos and it worked, the only problem is that in their video classes has nothing talking about how to display this data in textview or a listview ... you could help me with this, or will shed some video lesson on your channel about it ??? – Matheus Oliveira Sep 25 '16 at 19:42
  • give me your team viewer accesss i can show you how to show data to textview – Hamza Sep 25 '16 at 19:47
  • id:327 549 865 pw:5047 – Matheus Oliveira Sep 25 '16 at 19:56
  • let me downlaod the TV – Hamza Sep 25 '16 at 19:57
  • https://www.facebook.com/ameerhamza6733 come on fb , and TV not connecting – Hamza Sep 25 '16 at 20:12