在ListView Firebase数据库中列出具有特定子值的用户

时间:2018-09-12 09:14:34

标签: android firebase firebase-realtime-database

我正在尝试将Firebase实时数据库中子session: 2011的所有用户列出到ListView中。这是我的数据结构:

{
  "Users" : {
    "uid1" : {
      "name" : "Jhon",
      "session" : 2011
    },
    "uid2" : {
      "name" : "Kim",
      "session" : 2011
    }
  }
}

这是我的列表活动:

public class ListActivity extends AppCompatActivity {

    ListView lv;
    FirebaseListAdapter adapter;
    private FirebaseAuth mAuth;

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

        mAuth = FirebaseAuth.getInstance();

        FirebaseUser user = mAuth.getCurrentUser();
        String userID = user.getUid();
        DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
        ref.child(userID).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String session = dataSnapshot.child("session").getValue().toString();
                if (session.equals("2011")) {
                    Query query = FirebaseDatabase.getInstance().getReference().child("Users");

                    lv = (ListView) findViewById(R.id.listView);
                    FirebaseListOptions < profile > options = new FirebaseListOptions.Builder < profile > ()
                        .setLayout(R.layout.profile)
                        .setQuery(query, profile.class)
                        .build();
                    adapter = new FirebaseListAdapter(options) {
                        @Override
                        protected void populateView(View v, Object model, int position) {
                            TextView name = v.findViewById(R.id.name);
                            TextView session = v.findViewById(R.id.session);

                            profile user = (profile) model;
                            name.setText("Name: " + user.getName().toString());
                            session.setText("Session: " + user.getSession().toString());
                        }
                    };
                    lv.setAdapter(adapter);
                } else {
                    //
                }
            }
            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

这是我的profile班:

public class profile {
    private String name;
    private String session;

    public profile() {}

    public profile(String name, String session) {
        this.name = name;
        this.session = session;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSession() {
        return session;
    }

    public void setSession(String session) {
        this.session = session;
    }

}

但是问题是,它没有返回任何东西。我在这里想念什么?

为便于参考,我尝试了以下操作,但是当我这样做时,它仅返回当前登录用户的值。除此之外。

Query query = FirebaseDatabase.getInstance().getReference().child("Users");

lv = (ListView) findViewById(R.id.listView);
FirebaseListOptions < profile > options = new FirebaseListOptions.Builder < profile > ()
    .setLayout(R.layout.profile)
    .setLifecycleOwner(IdCheckActivity.this)
    .setQuery(query, profile.class)
    .build();
adapter = new FirebaseListAdapter(options) {
    @Override
    protected void populateView(View v, Object model, int position) {
        TextView name = v.findViewById(R.id.name);
        TextView session = v.findViewById(R.id.session);

        profile user = (profile) model;
        name.setText("Name: " + user.getName().toString());
        session.setText("Session: " + user.getSession().toString());
    }
};
lv.setAdapter(adapter);
}

@Override
protected void onStart() {
    super.onStart();
    adapter.startListening();
}

@Override
protected void onStop() {
    super.onStop();
    adapter.startListening();
}

1 个答案:

答案 0 :(得分:1)

您可以很好地使用Query,而不必使用DatabaseReference,并且可以在orderByChild()的帮助下完成以下操作:

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



rootRef.child("Users").orderByChild("session").equalTo(2011).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for(DataSnapshot data: dataSnapshot.getChildren()){
                    String tempName = data.child("name").getValue(String.class);

                   //with tempName you can access their usernames and you will only get the usernames with session 2011, so you can directly populate your listView from here and use it

                }

            }

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

            }
        });

tempName变量将仅获得name子级的Users,其中session为2011。同样如果将2011存储为String,请使用就像“ 2011”。

编辑:另外,如果您不听更改,只是想查看是否存在这样的User,则可以在if(dataSnapshot.exists())中使用valueEventListener()

使用此代码填充listView很简单,并且非常类似于您要做的事情,它看起来像这样:

ArrayList array = new ArrayList<String>;
ListView listView;

//set the findViewById for the listView

//add the following code in the valueEventListener

array.add(tempName);

ArrayAdapter adapter = new ArrayAdapter(YourActivity.this, android.R.layout.simple_list_item_1, array);
                listView.setAdapter(adapter);