简体   繁体   中英

ListView making in Firebase Android

I have a Firebase database with few values of customers as shown below. (I mentioned the child in brackets)

-Sooraj (child(registered user))
     -January(child(month))
           -Aswathy(child(name))
               -address: ""
               -cost: ""
               -details: ""
               -email: ""
               -name: "Aswatyy" - (child ("name"))-
               -phone: "944346848494"
               -received: ""
               -status: "Proposal Sent"

I need to take the child ("name") from database and put it in a ListView .

My declaration as

ListView customerList;
List<String> customerArray = new ArrayList<>();
ArrayAdapter<String> adapter;

then

customerList=findViewById(R.id.customerlistView);

ListView populating code as

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(month);
    DatabaseReference NameRef = ref.child("name");

    NameRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

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

                String nameDatabase = postSnapshot.getValue(String.class);
                customerArray.add(nameDatabase);
                adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, customerArray);
                customerList.setAdapter(adapter);
            }

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

            Log.w("name","load:onCancelled",databaseError.toException());

        }
    });

Nothing happening. Where I went wrong in this code?

If you want a list of names then try the following:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child(registeredUser);
 DatabaseReference nameRef = ref.child(month);

NameRef.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

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

            String name=postSnapshot.getKey();
            customerArray.add(name);
            adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, customerArray);
            customerList.setAdapter(adapter);
        }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

        Log.w("name","load:onCancelled",databaseError.toException());

    }
});

Assuming you have this db:

Sooraj
   January
      Aswathy
         address:...
         name:...

getKey() will retrieve the names in the database

  final ArrayList<String> names= new ArrayList<>();
        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = firebaseDatabase.getReference("User").child(userName).child(monthName);

        final ValueEventListener valueEventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for(DataSnapshot ds : dataSnapshot.getChildren()) {
                    HashMap nameModel = (HashMap) dataSnapshot.getValue();
                    String name =(String) nameModel.get("name");
                    names.add(name);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        databaseReference.addValueEventListener(valueEventListener);

from above method you can get the name array and you can set it to your adapter

Your table will be like this

User
   -registeredUserName
            -monthName
              -childName
                   name

if you do not have User as a table name

 -registeredUserName
            -monthName
              -childName
                   name

then just pass

DatabaseReference databaseReference = firebaseDatabase.getReference().child(userName).child(monthName);

In order to display all those names, please use the following code:

DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
DatabaseReference monthRef = rootRef.child(registeredUser).child(month);
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        List<String> list = new ArrayList<>();
        for(DataSnapshot ds : dataSnapshot.getChildren()) {
            String name = ds.child("name").getValue(String.class);
            list.add(name);
        }
        ListView listView = (ListView) findViewById(R.id.list_view);
        ArrayAdapter arrayAdapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, list);
        listView.setAdapter(arrayAdapter);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}
};
monthRef.addListenerForSingleValueEvent(eventListener);

In which registeredUser as in your example is Sooraj and the month is January .

The output will be:

Aswatyy
//All the other names

In your code, there are two major issues. First one would be your reference, which in ref is missing a child and in the NameRef points to property which is useless. The second problem is that you are creating and setting the adapter inside the for loop which will end up having a single item displayed (the last item).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM