2

I managed to get data from the Firebase Database and show it in an alphabetical order in my ListView.

Now I want to show the value from my database, if I click on an item in the ListView. As an example in my database it says "BB" as a name and the value is "Bye, bye".

So after an onClick event in the ListView a Toast message should show the value. How can I do this?

Here´s my database:

Database

ListView

Cyb3rKo
  • 413
  • 7
  • 22

2 Answers2

2

To show the value after you click on an item:

DatabaseReference ref=FirebaseDatabase.getInstance().getReference();
 listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
            final String selectedFromList = (String) list.getItemAtPosition(position);

            ref.orderByChild(selectedFromList).addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                 String retrievedValue=dataSnapshot.child(selectedFromList).getValue().toString();
                 Toast.makeText(activity_name.this, "Value: "+retrievedValue, Toast.LENGTH_LONG).show();

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

Assuming you have a listview with the following:

Afk
MFG

After clicking on an item, get the item at that postion and use it in a query orderByChild and retrieve it's value.

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • Hello again, I used your code in my App but no some errors appear and the app crashes. Why? You can see the errors in the picture at the top. – Cyb3rKo Apr 13 '18 at 20:08
  • what do you have in the listview? – Peter Haddad Apr 14 '18 at 08:08
  • I have uploaded a picture of it. As you can see I´ve edited my data in my database that the data that should be shown by the Toast is on the left and the data for the listview is on the right. Is this the problem? – Cyb3rKo Apr 14 '18 at 11:34
  • try `ref.orderByChild("Bye, bye").equalTo(selectedFromList)` and click on `BB` – Peter Haddad Apr 14 '18 at 11:39
  • you need to change the database to have the same key example `name: BB` and `name:XOXO` then you can just do `orderByChild("name").equalTo(selectedFromList)`. Change the database and use randomids also using `push()` – Peter Haddad Apr 15 '18 at 07:39
  • How do I use push? – Cyb3rKo Apr 15 '18 at 08:35
  • when saving to the database, do like this `DatabaseReference ref=FirebaseDatabase.getInstance().getReference().child("Message").push(); ref.child("name").setValue("BB");..//etc` – Peter Haddad Apr 15 '18 at 08:37
1

The sample code 'Peter Haddad' provided in his answer basically works fine... IF you design your database in a better way I did back then.
If you use his code, you get the value of the database entry (so the text on the right side in the console database, but I wanted to get the left one.


I recommend using a structure like in the Firebase Docs about Structuring.
It could look something like this:

enter image description here

Here are two posts about searching and querying those kind of databases:


If you anyways want to do it in the way I tried, it`s possible:

In the sample code from 'Peter Haddad' simply replace dataSnapshot.child(selectedFromList).getValue().toString() with dataSnapshot.child(selectedFromList).getKey().toString().

The key represents the text on the left side of the console database structure.

Cyb3rKo
  • 413
  • 7
  • 22