从Firebase检索但未显示的数据

时间:2017-10-25 09:19:27

标签: android listview firebase firebase-realtime-database data-retrieval

这是我的firebase数据库,我想在ListView点击此处查看所有大学名称点击此处查看图片

This is my firebase database from where I want to retrieve all the names of universities in <code>ListView</code> click here to see image

   myRef= FirebaseDatabase.getInstance().getReference();
   myRef.child("Universities").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                database c = postSnapshot.getValue(database.class);
                final String name = c.getuniName();
                userNameList.add(name);

                final ArrayAdapter<String> mutahirAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1 , userNameList);
                mListView.setAdapter(mutahirAdapter);

            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });

数据库类 我直接在Firebase中添加了数据我现在无法获取ListView中的数据我想要检索ListView

中的所有大学儿童
package com.mutahir.futureguide;

public class database {
    String Name;
    public String uniName(String Name) {
        this.Name= Name;
        return Name;
    }

    public String getuniName() {
        return Name;
    }

    public void setuniName(String Name) {
        this.Name = Name;
    }
}

1 个答案:

答案 0 :(得分:2)

希望它有效!

myRef= FirebaseDatabase.getInstance().getReference();
myRef.child("Universities").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {

        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
            final String name = postSnapshot.getValue("name").toString();
            userNameList.add(name);
        }
        if(!userNameList.isEmpty()){
            final ArrayAdapter<String> mutahirAdapter=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1 , userNameList);
            mListView.setAdapter(mutahirAdapter);
       }

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
    }
});