Firebase按钮onClick,设置UID子级

时间:2018-10-18 04:41:41

标签: firebase android-studio firebase-realtime-database

我在Firebase上有问题!,我已经创建了注册和登录页面,并且工作正常!!但我有一个问题。 why my button create a new 'child'? 如何通过单击按钮识别用户,将按钮“ onClick”放在相同的UID上? What I want to do,我不知道firebase的工作原理100%谁能为我解释?我真的需要{get}确实要单击按钮以标识数据库中的每个用户。 My page button

MainActivity

public class MainActivity extends AppCompatActivity {
private Button influenciador;
private Button marca;

private DatabaseReference mDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    influenciador = (Button) findViewById(R.id.BotaoSouInfluenciador);
    marca = (Button) findViewById(R.id.BotaoSouMarca);

    mDatabase = FirebaseDatabase.getInstance().getReference().child("Usuários");

    influenciador.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mDatabase.child("Usuários").push().setValue("Pizza");
            openDIContrato();
        }
    });

    marca.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mDatabase.child("Usuários").setValue("Burger");

            openMarcaContrato();
        }
    });
  }
public void openDIContrato() {
    Intent intent = new Intent(this, DIContrato.class);
    startActivity(intent);
  }

public void openMarcaContrato() {
    Intent intent = new Intent(this, MarcaContrato.class);
    startActivity(intent);
 }
}

用户类别

public class Usuarios {

private String id;
private String email;
private String aniversario;
private String senha;
private String nome;
private String sexo;

public Usuarios() {
}

public void salvar(){
    DatabaseReference referenciaFirebase = ConfiguracaoFirebase.getFirebase();
    referenciaFirebase.child("Usuários").child(String.valueOf(getId())).setValue(this);
}

@Exclude

public Map<String, Object> toMap(){
    HashMap<String, Object> hashMapUsuario = new HashMap<>();
    hashMapUsuario.put("id", getId());
    hashMapUsuario.put("email", getEmail());
    hashMapUsuario.put("aniversario", getAniversario());
    hashMapUsuario.put("nome", getnome());
    hashMapUsuario.put("sexo", getSexo());

    return hashMapUsuario;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getSenha() {
    return senha;
}

public void setSenha(String senha) {
    this.senha = senha;
}

public String getnome() {
    return nome;
}

public void setnome(String nome) {
    this.nome = nome;
}

public String getAniversario() {
    return aniversario;
}

public void setAniversario(String aniversario) {
    this.aniversario = aniversario;
}

public String getSexo() {
    return sexo;
}

public void setSexo(String sexo) {
    this.sexo = sexo;
 }
}

1 个答案:

答案 0 :(得分:0)

您没有使用对数据库的正确引用。从标题为“我想做什么”的图像中,您可以使用以下代码来做到这一点:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Usuários");

ref.child(iD).child("Button").setValue("Pizza");

//id is the string I can see in the databse image dG9ue...