gpt4 book ai didi

java - 如何在 Android 中读取 Firebase 用户数据

转载 作者:行者123 更新时间:2023-12-01 16:42:05 26 4
gpt4 key购买 nike

如何获取值

-M7U58NYUAHIGAQSlWQx

所以,当我运行最后一个 Activity 时,它得到 -M7U58NYUAHIGAQSlWQx但在它下面,却没有!

它就在“数据”字段下方,我可以读取并显示生日、电子邮件、姓名和电话!

例如values

我做错了什么?

{
"users" : {
"f1UXj3DTFFVaCNFK73roMMjMa5F3" : {
"bDay" : "99/99/9999",
"data" : {
"2020" : {
"5" : {
"-M7U58NYUAHIGAQSlWQx" : {
"nome" : "2",
"valor" : "2"
}
}
}
},
"email" : "99@gmail.com",
"name" : "Bruno",
"phone" : "999999999"
}
}
}

我一整天都在努力寻找,但没有人对我有同样的怀疑......

因此,这是造成这种情况的 3 个类:

AutoUpdate.java 是创建以下“数据”的基础。

package com.example.gestor_contas.TESTE;

import com.example.gestor_contas.DataCenter.Usuario;

import java.util.Calendar;


public class AutoUpdate extends Usuario {


private String ano;
private String mes;
private String nome;
private String valor;

public AutoUpdate(){

}
public String getNome() {
return nome;
}

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

public String getValor() {
return valor;
}

public AutoUpdate(String ano) {
this.ano = ano;
}

public void setValor(String valor) {
this.valor = valor;
}

public AutoUpdate(String nome, String valor) {
this.nome = nome;
this.valor = valor;
}

protected String updateAno(){
return String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
}
protected String updateMes(){
int aux = Calendar.getInstance().get(Calendar.MONTH) + 1;
return String.valueOf(aux);
}

}

此 Activity 负责添加具有新值的新“ HashMap ”。

package com.example.gestor_contas.TESTE;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.gestor_contas.Base.BaseActivity;
import com.example.gestor_contas.R;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.HashMap;
import java.util.Map;

public class TesteAdicionar extends BaseActivity{

private EditText mNome;
private EditText mValor;
private Button mEnviar;
private String ano;
private String mes;
private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference mDatabase ;

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

mNome = findViewById(R.id.fieldNome);
mValor = findViewById(R.id.fieldValor);
mEnviar = findViewById(R.id.buttonEnviar);
AutoUpdate up = new AutoUpdate();

ano = up.updateAno();
mes = up.updateMes();
mDatabase = database.getReference("users/" + getUid() + "/data/" + ano+ "/" + mes+"/");
mEnviar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
submitPost();
}
});
}
private void submitPost() {
final String nomeNF = mNome.getText().toString();
final String valorNF = mValor.getText().toString();
Toast.makeText(this, "Posting...", Toast.LENGTH_SHORT).show();
writeNewPost(nomeNF,valorNF);
}

private void writeNewPost(String name, String value) {
AutoUpdate up = new AutoUpdate(name,value);
String key = mDatabase.push().getKey();
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put(key, up);
mDatabase.updateChildren(childUpdates);
Toast.makeText(this, "Posted!", Toast.LENGTH_SHORT).show();
}
}

最后一个只负责可视化并从数据库获取

package com.example.gestor_contas.TESTE;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.gestor_contas.Base.BaseActivity;
import com.example.gestor_contas.R;
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 TesteVisualizar extends BaseActivity {

private FirebaseDatabase database = FirebaseDatabase.getInstance();
private DatabaseReference mDatabase;
private String ano;
private String mes;

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

AutoUpdate up = new AutoUpdate();
ano = up.updateAno();
mes = up.updateMes();

mDatabase = database.getReference("users" + "/" + getUid() + "/" + "data" + "/" + ano + "/" + mes + "/");

mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(TesteVisualizar.this, "3", Toast.LENGTH_SHORT).show();
}
});

}

private void showData(DataSnapshot dataSnapshot) {



for(DataSnapshot ds : dataSnapshot.getChildren()){
Toast.makeText(TesteVisualizar.this, "Inicio", Toast.LENGTH_SHORT).show();
Toast.makeText(TesteVisualizar.this, ds.getKey(), Toast.LENGTH_SHORT).show();

AutoUpdate up = new AutoUpdate();
up.setNome(dataSnapshot.getValue(AutoUpdate.class).getNome());
Toast.makeText(TesteVisualizar.this, up.getNome(), Toast.LENGTH_SHORT).show();


Toast.makeText(TesteVisualizar.this, "Fim", Toast.LENGTH_SHORT).show();
}

}
}

最佳答案

使用

up.setNome(ds.getValue(AutoUpdate.class).getNome());

而不是

up.setNome(dataSnapshot.getValue(AutoUpdate.class).getNome());

dataSanpshot:

"5" : {
"-M7U58NYUAHIGAQSlWQx" : {
"nome" : "2",
"valor" : "2"
}
}

ds:

"-M7U58NYUAHIGAQSlWQx" : {
"nome" : "2",
"valor" : "2"
}

因此您无法将 dataSnapshot 转换为 AutoUpdate.class。如果需要,可以转换为HashMap:

Map<String, Object> td = (HashMap<String, Object>) dataSnapshot.getValue();

关于java - 如何在 Android 中读取 Firebase 用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61843452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com