我如何使用小于或等于字符串?

时间:2018-01-28 17:19:42

标签: java android

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) {

                }
            });

1 个答案:

答案 0 :(得分:0)

user_bal是String数据类型,因此< =不是首选。如果你确定这个变量的值是数字(变量lim的整数是int类型,用于比较),那么试试这个

 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()

    }