【问题标题】:How can i use less than or equal to for a string? [closed]我如何使用小于或等于字符串? [关闭]
【发布时间】:2018-01-28 17:19:41
【问题描述】:
mUserBalance.child(user.getUid()).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    int lim = 100;

                    //final String user_bal = String.valueOf(dataSnapshot.child("balance").getValue());

                    //balance from firebase database
                    String user_bal = (String) dataSnapshot.child("balance").getValue();

                    if (user_bal.){
                        Toast.makeText(PostActivity.this, user_bal, Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(PostActivity.this, "not enough balance", Toast.LENGTH_LONG).show();
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

【问题讨论】:

  • 在做 Android 之前,您可能需要阅读 Java 教程

标签: java android


【解决方案1】:

user_bal 是 String 数据类型,因此

 String user_bal = (String) dataSnapshot.child("balance").getValue();
    int userBalance = Integer.parseInt(user_bal);

    if (userBalance  <= lim){
        // do something
    Toast.makeText(PostActivity.this, " NOt enough balance", Toast.LENGTH_LONG).show(); 


    }else{
       // do something
    Toast.makeText(PostActivity.this, user_bal, Toast.LENGTH_LONG).show()

    }

【讨论】:

  • 根据firebase数据,余额也可以自己作为双精度/整数获取
  • 什么意思?
  • @cricket_007:您需要定义它是否应该是双精度/整数。为了方便接下来进行的比较。数据以字符串形式存储。所以将字符串转换为双精度/整数并使 lim 匹配相同的数据类型。
  • 我只是说("balance").getValue(Integer.class); 是可能的
  • 这些是 firebase 支持的数据类型 String Long Double Boolean Map List 我怀疑你可以使用整数。在这种情况下,您可以使用 double 和
猜你喜欢
  • 1970-01-01
  • 2016-12-27
  • 1970-01-01
  • 2014-05-25
  • 2016-04-06
  • 2017-02-10
  • 1970-01-01
  • 2013-10-09
  • 2019-12-07
相关资源
最近更新 更多