android - Firebase 没有从孩子那里提取信息
问题描述
我正在尝试通过搜索名称从firebase生成带有详细信息的卡片信息,然后,我正在绘制带有二维码的卡片。
我尝试通过更改路径但它仍然返回 null 并且我尝试使用查询但它仍然返回 null
JAVA
public class creare_card extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
EditText idInput;
Button btCardGen;
ImageView ivCard;
DatabaseReference mdatabaseref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creare_card);
firebaseAuth=FirebaseAuth.getInstance();
final FirebaseUser users=firebaseAuth.getCurrentUser();
String finaluser=users.getEmail();
String resultemail=finaluser.replace(".","");
mdatabaseref= FirebaseDatabase.getInstance().getReference("Pacienti/ID");
idInput=findViewById(R.id.inputID);
btCardGen=findViewById(R.id.generareCard);
ivCard=findViewById(R.id.imagView_card);
btCardGen.setOnClickListener(v -> {
String sText=idInput.getText().toString().trim();
MultiFormatWriter writer=new MultiFormatWriter();
Bitmap bitmap = null;
try {
BitMatrix matrix=writer.encode(sText, BarcodeFormat.QR_CODE,250,250);
BarcodeEncoder encoder=new BarcodeEncoder();
bitmap=encoder.createBitmap(matrix);
ivCard.setImageBitmap(bitmap);
InputMethodManager manger=(InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE
);
manger.hideSoftInputFromWindow(idInput.getApplicationWindowToken(),0);
} catch (WriterException e) {
e.printStackTrace();
}
// DB QUERY
Bitmap finalBitmap = bitmap;
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
String nume = "";
System.out.println(dataSnapshot.getKey());
for ( DataSnapshot child: dataSnapshot.getChildren() )
{
System.out.println("child");
System.out.println(child.getKey());
System.out.println(child.getValue());
if ( child.getKey() == "numeP" )
nume = (String)child.getValue();
}
//DESENEAZA CARDUL
Bitmap src = finalBitmap; // the original file yourimage.jpg i added in resources
Bitmap dest = Bitmap.createBitmap(src.getWidth()+230, src.getHeight()+50, Bitmap.Config.ARGB_8888);
String yourText = nume;
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(35);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
cs.drawBitmap(src, 200f, 25f, null);
float height = tPaint.measureText("yY");
float width = tPaint.measureText(yourText);
float x_coord = (src.getWidth() - width)/2;
cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
ivCard.setImageBitmap(dest);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
System.out.println("Eroare baza de date");
}
};
mdatabaseref.orderByKey().addValueEventListener(postListener);
});
}
}
public class creare_card extends AppCompatActivity {
private FirebaseAuth firebaseAuth;
EditText idInput;
Button btCardGen;
ImageView ivCard;
DatabaseReference mdatabaseref;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_creare_card);
firebaseAuth=FirebaseAuth.getInstance();
final FirebaseUser users=firebaseAuth.getCurrentUser();
String finaluser=users.getEmail();
String resultemail=finaluser.replace(".","");
mdatabaseref= FirebaseDatabase.getInstance().getReference("Pacienti/ID");
idInput=findViewById(R.id.inputID);
btCardGen=findViewById(R.id.generareCard);
ivCard=findViewById(R.id.imagView_card);
btCardGen.setOnClickListener(v -> {
String sText=idInput.getText().toString().trim();
MultiFormatWriter writer=new MultiFormatWriter();
Bitmap bitmap = null;
try {
BitMatrix matrix=writer.encode(sText, BarcodeFormat.QR_CODE,250,250);
BarcodeEncoder encoder=new BarcodeEncoder();
bitmap=encoder.createBitmap(matrix);
ivCard.setImageBitmap(bitmap);
InputMethodManager manger=(InputMethodManager) getSystemService(
Context.INPUT_METHOD_SERVICE
);
manger.hideSoftInputFromWindow(idInput.getApplicationWindowToken(),0);
} catch (WriterException e) {
e.printStackTrace();
}
// DB QUERY
Bitmap finalBitmap = bitmap;
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
String nume = "";
System.out.println(dataSnapshot.getKey());
for ( DataSnapshot child: dataSnapshot.getChildren() )
{
System.out.println("child");
System.out.println(child.getKey());
System.out.println(child.getValue());
if ( child.getKey() == "numeP" )
nume = (String)child.getValue();
}
//DESENEAZA CARDUL
Bitmap src = finalBitmap; // the original file yourimage.jpg i added in resources
Bitmap dest = Bitmap.createBitmap(src.getWidth()+230, src.getHeight()+50, Bitmap.Config.ARGB_8888);
String yourText = nume;
Canvas cs = new Canvas(dest);
Paint tPaint = new Paint();
tPaint.setTextSize(35);
tPaint.setColor(Color.BLUE);
tPaint.setStyle(Paint.Style.FILL);
cs.drawBitmap(src, 200f, 25f, null);
float height = tPaint.measureText("yY");
float width = tPaint.measureText(yourText);
float x_coord = (src.getWidth() - width)/2;
cs.drawText(yourText, x_coord, height+15f, tPaint); // 15f is to put space between top edge and the text, if you want to change it, you can
ivCard.setImageBitmap(dest);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
System.out.println("Eroare baza de date");
}
};
mdatabaseref.orderByKey().addValueEventListener(postListener);
});
}
}
我正在尝试通过搜索名称从firebase生成带有详细信息的卡片信息,然后,我正在绘制带有二维码的卡片。
我尝试通过更改路径但它仍然返回 null 并且我尝试使用查询但它仍然返回 null
JSON Firebase
{
"user" : {
"Pacienti" : {
"ID" : { //this is returning null
"ageP" : "21",
"cnpP" : "312312312312",
"numeP" : "salut",
"prenP" : "sunteu",
"qrcode" : "ID",
"simpP" : "are"
}
}
}
}
解决方案
这一行:
mdatabaseref= FirebaseDatabase.getInstance().getReference("Pacienti/ID");
将您的参考设置为/Pacienti/ID
(如果它返回则不存在null
)。
您想要的参考是/user/Pacienti/ID
,它将使用:
mdatabaseref= FirebaseDatabase.getInstance().getReference("user/Pacienti/ID");
推荐阅读
- asp.net-mvc - IIS 中托管的 React.js ASP.NET MVC 核心应用程序无法读取 API(控制器)404
- c# - 使用 linq c# 进行联合
- python - 如何从方阵中获取有关元素降序的信息
- dom-events - 如何在 addEventListener 中安排 removeEventListener?
- react-native - React 本机更新状态“自行”
- sonos - 在为有声读物禁用的曲目中搜索
- node.js - 如何解决下一个不是函数的错误信息?
- excel - 根据另一列中的单元格值验证一列中的单元格值
- python - 如何使用参数在后台线程中从不同的类运行方法?
- regex - 计算A列中有多少也在B列中