0

This is my firebase database: enter image description here

My database reference:

DatabaseReference mReference = FirebaseDatabase.getInstance().getReference();

In android app, what I want is to just retrieve the child nodes(the "red" ones). I don't want to retrieve its inner children if that's possible.

Here is my code:

private ListView userList;
private ArrayList<String> usernames = new ArrayList<>();
private ArrayList<String> keys = new ArrayList<>();

private DatabaseReference mDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    userList = (ListView) findViewById(R.id.usernames);

    final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, usernames);
    userList.setAdapter(arrayAdapter);

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

    mDatabase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

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

                usernames.add(childNodes.getValue(String.class));
                arrayAdapter.notifyDataSetChanged();
            }

        }
     }
  }

I only want to list the node names in the android listview, whether retrieving all its children or not. Is there any way? I tried 'getChildren()' method but it retrieves the all children (lat, long) but not the bus_01, bus_02 and bus_03. Any Help?

Sagaryal
  • 415
  • 4
  • 15
  • There is no way within the Firebase SDK to read a node without its contents. See https://stackoverflow.com/questions/41590730/firebase-shallow-query-parameter-for-android – Frank van Puffelen Jul 15 '17 at 03:30
  • Ok, then , how can I list the nodes in android listview, only nodes not its children, though i may have to retrieve all the children. Is there a way? – Sagaryal Jul 15 '17 at 03:31
  • This sounds like an [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Update your question with the [minimal code that reproduces where you are stuck](http://stackoverflow.com/help/mcve). – Frank van Puffelen Jul 15 '17 at 03:39
  • Mr. Frank, I've updated my question. If any problem again, please help me to correct my question. Thanks. – Sagaryal Jul 15 '17 at 03:47
  • To get the names, use `usernames.add(childNodes.getKey());` – Frank van Puffelen Jul 15 '17 at 13:49

0 Answers0