Home > OS >  When scanning a QR code I want to check if the value exists in my database
When scanning a QR code I want to check if the value exists in my database

Time:02-16

I have implemented a QR code scanner that when I scan a QR code I get an AlertDialog. What I want is when I scan a QR code with the value of 2 (Example), I want the application to check if the value exists in my Database. If it does, I want to set the title in my AlertDialog to the "toolname" value from my Database.

I have actually done this before, but since I'm new to this I don't remember how I did it. Hopefully, someone could help me out.

Here is my structure:

Firebase Firebase database

Here is my code for the QR scanner:

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);


    IntentResult intentResult = IntentIntegrator.parseActivityResult(
            requestCode, resultCode, data
    );


    if (intentResult.getContents() != null) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

        builder.setMessage("Hello");

        builder.setTitle("Here I want the name of the item in my database");

        builder.show();

    }
}

Here is how I read data from firebase:

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


    mAuth = FirebaseAuth.getInstance();
    databaseUsers = FirebaseDatabase.getInstance("").getReference("Ansatte");
    databaseTools = FirebaseDatabase.getInstance(").getReference("Fellesverktøy");


    currentUser = mAuth.getCurrentUser().getUid();

    DatabaseReference hasUserStorageRef = databaseUsers.child(currentUser).child("hasusersstorage");

    userloggedin = findViewById(R.id.tv_userloggedin);
    bttoaddtool = findViewById(R.id.bt_addtool);
    bttoollist = findViewById(R.id.bt_toollist);
    btusertoollist = findViewById(R.id.bt_usertoollist);
    btlogoutuser = findViewById(R.id.bt_logoutuser);
    ivqrscan = findViewById(R.id.iv_qrscan);

    hasUserStorageRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
        @Override
        public void onComplete(@NonNull Task<DataSnapshot> task) {
            if (task.isSuccessful()) {
                DataSnapshot snapshot = task.getResult();
                if (snapshot.exists()) {
                    String hasusersstorage = snapshot.getValue(String.class);
                    if (hasusersstorage.equals("Nei")) {

                        bttoaddtool.setVisibility(View.GONE);
                        btusertoollist.setVisibility(View.GONE);
                    } else if (hasusersstorage.equals("Ja")) {
                        bttoaddtool.setVisibility(View.VISIBLE);
                        btusertoollist.setVisibility(View.VISIBLE);
                    }
                }
            } else {
                Log.d("TAG", task.getException().getMessage());
            }
        }
    });

Updated code:

 @Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);

    Query query = databaseTools;

    query.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

            builder.setTitle(intentResult.getContents());

            String toolid = snapshot.getValue().toString();

            builder.setMessage(toolid);


            builder.show();

        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {

        }
    });
}

}

CodePudding user response:

To get the value of toolname field when searching the Firebase Database, please use the following lines of code:

DatabaseReference db = FirebaseDatabase.getInstance().getReference();
DatabaseReference productsRef = db.child("Fellesverktøy").orderByChild("toolQRid").equalTo("2");
    productsRef.get().addOnCompleteListener(new OnCompleteListener<DataSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<DataSnapshot> task) {
        if (task.isSuccessful()) {
            for (DataSnapshot ds : task.getResult().getChildren()) {
                String toolName = ds.child("toolname").getValue(String.class);
                Log.d("TAG", toolName);
            }
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});

The result in the logcat will be:

heipadeg

Now, you can take this value and add it as a title to your AlertDialog.

  • Related