Home » Java » java – To Fetch Row data fully from FirebaseRealtime-database by checking the product id (child id) with android app scan result id-Exceptionshub

java – To Fetch Row data fully from FirebaseRealtime-database by checking the product id (child id) with android app scan result id-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

my app is with barcode scanner and i have created Firebase Realtime-database for the app. i entered some data to database. i want to fetch all the data(name,details,price) in a row by checking the scanresult to the id(in a specific row) in the database. found some tutorials similar to query but i couldn’t set code as i wish. please, anyone help me to do this small correction for me ?

below i mentioned my present working code. but it is checking the scanresult with the row unique id and fetched as i want to do. but it is difficult to enter the data with same UID as same as scanresult. This code is working only when i dynamically add data directly to database in site.

The first pic is, of working present code:

enter image description here

the secong pic is to be fetch by checking its id :

enter image description here

@Override
public void handleResult(Result result) {
    final String scanResult = result.getText().toString();
    setContentView(R.layout.activity_second);


    databasefetch = FirebaseDatabase.getInstance().getReference().child("save").child(scanResult);
    databasefetch.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            TextView text = (TextView) findViewById(R.id.textView3);
            String name = dataSnapshot.child("name").getValue().toString();
            String details = dataSnapshot.child("details").getValue().toString();
            String price = dataSnapshot.child("price").getValue().toString();

            text.setText("\nProduct Name : "+name+"\nDetails : "+details+"\nPrice : "+price);

            //text to speech
            String  toSpeak=text.getText().toString();
            //Toast.makeText(getApplicationContext(),toSpeak,Toast.LENGTH_SHORT).show();
            txt.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);


        }

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

        }
    });
How to&Answers:

To get the data from the second JSON snippet based on its id property, you need to run a query:

databasefetch = FirebaseDatabase.getInstance().getReference().child("save");
Query query = databasefetch.orderByChild("id").equalTo(8901058862836L);
query.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
      for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
        TextView text = (TextView) findViewById(R.id.textView3);
        String name = snapshot.child("name").getValue(String.class);
        String details = snapshot.child("details").getValue(String.class);
        String price = snapshot.child("price").getValue(String.class);

        ...
      }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException(); // never ignore errors
    }
})