-1

As the title says, i just cannot find a solution. What I've tried so far, results in no success.

Here's a link to an image below.

Firebase database example

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
GojkoG
  • 3
  • 3
  • i can't understand what you want to do – Ravi Makvana Aug 30 '18 at 13:08
  • @NileshRathod: can you explain why this is a duplicate of the question you linked? I don't see the connection between the two. – Frank van Puffelen Aug 30 '18 at 13:11
  • @GojkoK: please edit your question to include what you've already tried. Without that it'll be hard to explain things better than the Firebase documentation already does. The best way to ask for help is by showing a [minimal, complete, standalone piece of code that reproduces what you tried and where you got stuck](http://stackoverflow.com/help/mcve) (read the link please, it's quite useful). – Frank van Puffelen Aug 30 '18 at 13:13
  • I have sections like Ada and then it's children. Now, i want to display all the children of each section on ListView. Then after i click on an item in the ListView that it adds some String to it. So for example i want to attach String " - Selected" to FIELD1 under number 7. – GojkoG Aug 30 '18 at 19:01

1 Answers1

0

If you want to get only the value of the FIELD1 property under node 7, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference fieldOneRef = rootRef.child("Ada").child("7").child("FIELD1");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        String field1 = dataSnapshot.getValue(String.class);
        Log.d(TAG, field1);
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
fieldOneRef.addListenerForSingleValueEvent(valueEventListener);

And if you want to get the value of the FIELD1 property under all nodes, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference adaRef = rootRef.child("Ada");
ValueEventListener valueEventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String field1 = ds.child("FIELD1").getValue(String.class);
            Log.d(TAG, field1);
        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.d(TAG, databaseError.getMessage());
    }
};
adaRef.addListenerForSingleValueEvent(valueEventListener);
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • That's great code, but how do i display it in the ListView? – GojkoG Aug 30 '18 at 18:49
  • This sounds to be another question but to solve it, I'll recommend see my answer from this **[post](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)** where I have explained, how you can get data from a Firebase realtime database and display it in a `ListView` using an `ArrayAdaper`. So you should make your own attempt given the information in that answer, and ask another question if something else comes up. – Alex Mamo Aug 30 '18 at 19:09
  • I've seen your link about ListView, it's very useful but not quite what i need. I want to set item in ListView so that when it's clicked, it changes from lets say "Alex Mamo" to "Alex Mamo - Selected". – GojkoG Aug 31 '18 at 08:46
  • You can simply achieve this using `setOnItemClickListener` and according to what is selected to trigger a specific operation. – Alex Mamo Aug 31 '18 at 08:55
  • I tried this, but it doesn't change the ` myListView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { Object item = myListView.getItemAtPosition(position); item = item + " - Selektovan/a!"; adapter.notifyDataSetChanged(); Toast.makeText(Bocac.this, ""+item, Toast.LENGTH_SHORT).show(); } }); ` – GojkoG Aug 31 '18 at 09:33
  • I understand but this sounds as a new issue which is basically another question that I cannot responde in a comment or using only the the data above. In order to follow the rules of this comunity, please post another fresh question using a [MCVE](https://stackoverflow.com/help/mcve), so me and other users can help you. – Alex Mamo Aug 31 '18 at 09:49
  • Can't seem to make another post today since I am limited. – GojkoG Aug 31 '18 at 13:23
  • You need to wait a couple of hours and you'll be able. These are stackoverflow rules. – Alex Mamo Aug 31 '18 at 13:24