Ad

Reading Database Title And Displaying In A List View In Android Studio Java

i would like to read the database child's title and display them in the database as a list view , how can this be possible ? here is my code that i tried but it dosnt work :/ i want to display the titles in blue circle , can any one help please as i need to finish this as soon as possible ? thanks in advance <3 enter image description here

 Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            selectedCity = bundle.getInt("city", 0);
            selectedCategory = bundle.getInt("category", 0);
        }

        dataListView =  findViewById(R.id.dataListView);
        database = FirebaseDatabase.getInstance();
        String _city = "";
        switch (selectedCity){
            case 1: _city = "Alexandria";
            break;
            case 2: _city = "Aswan";
                break;
            case 3: _city = "Cairo";
                break;
            case 4: _city = "Fayyoum";
                break;
            case 5: _city = "Giza";
                break;
            case 6: _city = "Hurghada";
                break;
            case 7: _city = "Luxor";
                break;
            case 8: _city = "Marsa Matruh";
                break;
            case 9: _city = "North Coast";
                break;
            case 10: _city = "Sharm El-Sheikh";
                break;
            case 11: _city = "Sharqia";
                break;
            case 12: _city = "Siwa";
                break;
            case 13: _city = "Taba";
                break;


        }
        databaseReference = database.getReference("Places/"+_city);








        adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_single_choice,
                cityItems);
        dataListView.setAdapter(adapter);
        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                String value = dataSnapshot.getValue(String.class);
                cityItems.add(value);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });

nothing get displayed tho

Ad

Answer

Try the following:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Places");

ref.addValueEventListener(new ValueEventListener(){

   @Override
   public void onDataChange(DataSnapshot dataSnapshot){
    for(DataSnapshot ds : dataSnapshot.getChildren()){
      String key = ds.getKey();
     }
  }

First add a reference to the node Places then loop inside the direct children and you will be able to retrieve the keys.

Ad
source: stackoverflow.com
Ad