java – i want to fetch data full in a single Textview from firebase database-ThrowExceptions

Exception or error:

I have created a barcode scanner app and it reads the barcode id and needed the output as voice output. in order to read all the things i need to fetch all data together in one single TextView. I did my level best to add the query from database. but it only shows in TextView as com.google.firebase.database.Query@9fc9e4d and my app TTS engine reads it clearly. But i need to set it as all data under a single TextView from database to speak out. please help me???? here only i attached the result handling method.

@Override 
public void handleResult(Result result) { final String scanResult = result.getText();

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



            setContentView(R.layout.activity_second);
            TextView text = (TextView) findViewById(R.id.textView3);
            String tst = databasefetch.child("save").orderByChild("id").equalTo(scanResult).toString();
            text.setText(tst);



            //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) {

        }
    });
}

enter image description here

enter image description here

How to solve:

No need to run multiple query to filter the data based on id. Check below:

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

        String  toSpeak = "";

        for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) {

            String name = childSnapshot.child("name").getValue(String.class);
            String price = childSnapshot.child("price").getValue(String.class);
            String details = childSnapshot.child("details").getValue(String.class);

            toSpeak = name + ", " + price + ", " + details;
        }

        text.setText(toSpeak);

        txt.speak(toSpeak, TextToSpeech.QUEUE_FLUSH,null);
    }

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

    }
});

###

Try something like this

@Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
            // TODO: handle the post
            YourModelClass object = postSnapshot.getValue(YourModelClass.class);
            text.setText(object.getId().equalsIgnoreCase(scanResult) ? object.getDescription() : "");
        }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *