繁体   English   中英

Switch Case 语句的问题

[英]Issue with Switch Case Statement

我有三种方法:

  1. 首先检查用户的性别
  2. 二查房屋分类类型
  3. 第三个将排序后的用户放入我的卡适配器

我的问题是第二种方法checkHousing() 如果用户选择同性住房,它会检查所有其他选择同性住房的男性。女性用户反之亦然。 到目前为止,Iv 只能获取所有选择了“Same Sex”的用户,包括男性和女性。

有什么建议? 我觉得有一种更有效的方法可以做到这一点,但似乎无法弄清楚。

顺便说一下,我使用 Firebase 实时数据库来存储用户的信息。

private String userSex;
private String thisUserSex;
public void checkUserSex() {
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    DatabaseReference userDb = usersDb.child(user.getUid());
    userDb.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                if (dataSnapshot.child("sex").getValue() != null) {
                    userSex = dataSnapshot.child("sex").getValue().toString();
                    switch (userSex) {
                        case "Male":
                            thisUserSex = "Male";
                            break;
                        case "Female":
                            thisUserSex = "Female";
                            break;
                      }
                    checkHousing();
                } } }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        } }); }

private String housingType;
private String assortment;
public void checkHousing() {
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    final DatabaseReference userDb = usersDb.child(user.getUid());
    userDb.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {
                if (dataSnapshot.child("Housing").getValue() != null) {
                    housingType = dataSnapshot.child("Housing").getValue().toString();
                    switch (housingType) {
                        case "Same Sex":
                            assortment = "Same Sex";
                            break;
                        case "Uni-Sex":
                            assortment = "Uni-Sex";
                            break;
                    }   if (assortment == "Same Sex") dataSnapshot.getValue().toString().equals(thisUserSex);

                        else { assortment = "UniSex";
                     }
                   }
                 }getRoomMates();
                }
                 @Override
                 public void onCancelled(DatabaseError databaseError) {
                }
            });
        }

       public void getRoomMates(){
         usersDb.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.child("Housing").getValue() != null) {
                    if (dataSnapshot.exists() && 
                     !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && 
                     !dataSnapshot.child("connections").child("yeps").hasChild(currentUId) && 
                     dataSnapshot.child("Housing").getValue().toString().equals(assortment)) {
                        String profileImageUrl = "default";
                        if (!dataSnapshot.child("profileImageUrl").getValue().equals("default")) {
                            profileImageUrl = 
                      dataSnapshot.child("profileImageUrl").getValue().toString();
                        }
                        cards item = new cards(dataSnapshot.getKey(), 
                        dataSnapshot.child("name").getValue().toString(), profileImageUrl);
                        rowItems.add(item);
                        arrayAdapter.notifyDataSetChanged();
                } } }

您可以将Same Sex拆分为Same SexMSame SexF并将它们Same SexF存储,然后根据数据库中的MF选择室友。 开关内部:


if(thisUserSex.equalsIgnoreCase("Male"){
  assortment = "Same SexM";
}else{
  assortment = "Same SexF";
}


检查数据库中的信息,我有一些实例,在运行时数据没有被更改,这可能是它总是返回相同结果的原因。 我知道您可以在大多数编辑器中使用调试模式,或者您可以打印出字符串外壳类型的当前值。

编辑:仔细查看您正在使用实时数据库的代码,以便检查您提交的信息是否得到正确处理。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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