【问题标题】:Function inside the card view Android卡片视图Android里面的功能
【发布时间】:2018-09-05 09:20:23
【问题描述】:

在下面的函数中,CardView 必须显示分数,但它不会根据值改变(如果相等的分数是 10,如果不是分数是 1。)

但我得到的只是难度值 1

public void getOppositeUsers22(){
        usersDb.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                if (dataSnapshot.child("post").getValue() != null) {
                    if (dataSnapshot.exists() && !dataSnapshot.child("connections").child("nope").hasChild(currentUId) && !dataSnapshot.child("connections").child("yeps").hasChild(currentUId) &&dataSnapshot.child("post").getValue().toString().equals(oppositeUser2)) {
                        String profileImageUrl = "default";
                        Integer score2 =  1;


                        if (!dataSnapshot.child("profileImageUrl").getValue().equals("default")) {
                            profileImageUrl = dataSnapshot.child("profileImageUrl").getValue().toString();
                             if (dataSnapshot.child("industry").getValue().equals(innduusry)) {
                                 score2 = 10;


                            }
                        }

                        score = String.valueOf(score2);


                        cards item = new cards(dataSnapshot.getKey(), dataSnapshot.child("name").getValue().toString(),score, profileImageUrl);
                        rowItems.add(item);
                        arrayAdapter.notifyDataSetChanged();
                    }
                }
            }

【问题讨论】:

  • 检查您的哪个 if 条件失败。
  • 这个失败了—— if (dataSnapshot.child("industry").getValue().equals(innduusry)) { score2 = 10; }
  • 打印dataSnapshot.child("industry").getValue()innduusry的值,看看是否相同
  • 是的,工业和工业的值相同,但在卡片视图中只显示 1 但必须为 10,就好像等于 score2 = 10;
  • 您确定if (dataSnapshot.child("industry").getValue().equals(innduusry) 条件检查失败了吗?那么这意味着dataSnapshot.child("industry").getValue() 不等于innduusry

标签: android firebase-realtime-database android-cardview


【解决方案1】:

这个方法调用 getOppositeUsers22()

public void checkUser(){
        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("post").getValue() != null){
                        innduusry = dataSnapshot.child("industry").getValue().toString();

                        oppositeUser2 = "post";


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

            }
        });
    }

【讨论】:

  • innduusry 是全局变量吗?
  • 是的。这是给将使用该应用程序的用户的。
  • 是的,但分数仍然显示为 1 而不是 10 偶数相等
  • score 是在跳过 if 条件,所以我需要改变 if 语句的位置吗?
  • 请检查您在dataSnapshot.child("industry").getValue().toString()innduusry 中获得的值是多少
【解决方案2】:

解决方案:

代替:

if (dataSnapshot.child("industry").getValue().equals(innduusry)) {
        score2 = 10;
}

写:

if (dataSnapshot.child("industry").getValue().toString().equals(innduusry)) {
        score2 = 10;
}

然后尝试。希望它有效。

【讨论】:

  • score 是 score2 的字符串值。它有什么不同?
  • 我稍后会添加更多 if 语句,因此 score2 是一个变量,将来会添加更多以获得总计。 score 将根据 score2 等整数变量进行总计算。 score 是要添加到卡片视图中的字符串变量
  • 将 score2 添加到卡片项目中会出现错误。 score 已经作为字符串添加到 java 类卡中。
  • 好的,innduusry@IndikaDharshana 是什么
  • if (dataSnapshot.child("post").getValue() != null) { innduusry = dataSnapshot.child("industry").getValue().toString();相反UserSex2 =“发布”; getOppositeUsers22(); }
猜你喜欢
  • 1970-01-01
  • 2016-01-07
  • 2016-10-13
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多