1
Bundle bundle = getIntent().getExtras();
   String name = bundle.getString("name");

   mValueView = (TextView) findViewById(R.id.textView);

   mRef = FirebaseDatabase.getInstance()
            .getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");

   com.google.firebase.database.Query query = mRef.child("Users").orderByChild("title").equalTo(name);

   query.addValueEventListener(new ValueEventListener() {
       @Override
       public void onDataChange(DataSnapshot dataSnapshot) {
           //Map<String,Object> map = (Map<String, Object>) dataSnapshot.getValue();
           //String Title = (String) map.get("title");
           String Title = dataSnapshot.child("title").getValue().toString();
           mValueView.setText(Title);

       }

       @Override
       public void onCancelled(DatabaseError databaseError) {

       }
   });

I want to show object title is same name value.

This is the Firebase database:

enter image description here

AL.
  • 36,815
  • 10
  • 142
  • 281
midtonight
  • 67
  • 1
  • 10

2 Answers2

1

If you want to search title value only one time, without listening for updates, you can simply use :

ref.addListenerForSingleValueEvent(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
    }});

Then if you get a reference to 'Users' you can do some logic with iteration, for example :

for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
    String title = (String) singleSnapshot.child("title").getValue();
                        //do your logic
                    }
1

There are two problems in your code:

  1. you're specifying the child node Users twice
  2. a query results a list of results, which your onDataChange doesn't handle

specifying the child node Users twice

mRef = FirebaseDatabase.getInstance()
         .getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/Users");
                                                               // ^^^^^ 

Query query = mRef.child("Users").orderByChild("title").equalTo(name);
                       // ^^^^^ 

Easily fixed:

mRef = FirebaseDatabase.getInstance()
         .getReferenceFromUrl("https://mymap-3fd93.firebaseio.com/");

Query query = mRef.child("Users").orderByChild("title").equalTo(name);

I'm not sure why you use getReferenceFromUrl() to begin with. For most applications, this accomplishes the same is simpler:

mRef = FirebaseDatabase.getInstance().getReference();

a query results a list of results

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

   query.addValueEventListener(new ValueEventListener() {
       @Override
       public void onDataChange(DataSnapshot dataSnapshot) {
           for (DataSnapshot snapshot: dataSnapshot.getChildren()) {
               System.out.println(snapshot.getKey());
               System.out.println(snapshot.child("title").getValue(String.class));
           }
       }

       @Override
       public void onCancelled(DatabaseError databaseError) {
           throw databaseError.toException();
       }
   });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807