简体   繁体   中英

i needed a way to check data, is that exist or not on realtime database

i having an qr code layout that been used for opening higher authority activity layout, and how can i make/search the code online on the firebase database that look like below here

this what my firebase data look

below this what the code that can open to new activity, but i want the code was different each admin, so if the admin retire i dont need to change the code inside the app everytime


public class BarcodeScanner extends AppCompatActivity {

    private CodeScanner mCodeScanner;
    private static final String Code = "newsenyum";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_barcode_scanner);
        CodeScannerView scannerView = findViewById(R.id.scanner_view);
        mCodeScanner = new CodeScanner(this, scannerView);
        mCodeScanner.setDecodeCallback(new DecodeCallback () {
            @Override
            public void onDecoded(@NonNull final Result result) {
               BarcodeScanner.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        String codeFromScanner = result.getText(); // initialize the code from scanner

                        if(Code.equals (codeFromScanner.trim ())){
                            redirectToNewActivity ();
                        }else{
                            Toast.makeText (BarcodeScanner.this, "QRCode Tidak Dapat di Gunakan", Toast.LENGTH_SHORT).show ( );
                        }
                    }
                });
            }
        });


        scannerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mCodeScanner.startPreview();
            }
        });
    }



    private void redirectToNewActivity() {
        startActivity (new Intent (getApplicationContext ( ), AdminUID.class));
        finish ( );
    }

    @Override
    protected void onResume() {
        super.onResume();
        mCodeScanner.startPreview();
    }

    @Override
    protected void onPause() {
        mCodeScanner.releaseResources();
        super.onPause();
    }

}

To check if a particular bar code exists in the database, you need to use a Query object. So please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference adminRef = db.child("Admin");
Query queryByCode = adminRef.orderByChild("barcode code").equalTo("pa29h9h75dg");
queryByCode.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            DataSnapshot snapshot = task.getResult();
            if(snapshot.exists()) {
                Log.d("TAG", "Barcode already exists.");
            } else {
                Log.d("TAG", "Barcode doesn't exist.");
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Don't ignore potential errors!
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM