首页 > 解决方案 > 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"
      }
    }
  }
}

标签: androidfirebasefirebase-realtime-database

解决方案


这一行:

mdatabaseref= FirebaseDatabase.getInstance().getReference("Pacienti/ID");

将您的参考设置为/Pacienti/ID(如果它返回则不存在null)。

您想要的参考是/user/Pacienti/ID,它将使用:

mdatabaseref= FirebaseDatabase.getInstance().getReference("user/Pacienti/ID");

推荐阅读