[英]How to display decimal value from firestore database to android studio
I'm trying to show the points from my firestore database unto my android application but it only shows whole number instead of actually including the decimal numbers.我试图将我的 firestore 数据库中的点显示到我的 android 应用程序,但它只显示整数而不是实际包含十进制数。 Firestore database: https://imgur.com/J6y9Tsg Android application: https://imgur.com/5CuBvDW
Firestore 数据库: https://imgur.com/J6y9Tsg Android 应用程序: https://imgur.com/5CuBvDW
I have read previous posts about format("#.##") or DecimalFormat("#,###.####") but I'm not entirely sure how to apply that to my code.我已经阅读过有关 format("#.##") 或 DecimalFormat("#,###.####") 的先前帖子,但我不完全确定如何将其应用于我的代码。
public class MainActivity extends AppCompatActivity {
private TextView powents;
FirebaseFirestore fStore;
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button logout = findViewById(R.id.logout);
ImageView qrimage = findViewById(R.id.qrimage);
powents = findViewById(R.id.powents);
fStore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
FirebaseUser user = fAuth.getCurrentUser();
if (user != null) {
String mail = user.getEmail();
DocumentReference docref = fStore.collection("Users").document(mail);
docref.addSnapshotListener(this, (documentSnapshot, error) -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
powents.setText(String.valueOf(Math.toIntExact(documentSnapshot.getLong("Points"))));
}
});
}
try {
BarcodeEncoder barcode = new BarcodeEncoder();
Bitmap bitmap = barcode.encodeBitmap(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getEmail(), BarcodeFormat.QR_CODE, 650, 650);
qrimage.setImageBitmap(bitmap);
} catch (Exception e) {
e.printStackTrace();
}
logout.setOnClickListener(view -> {
FirebaseAuth.getInstance().signOut();
Toast.makeText(MainActivity.this, "Logged Out!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this, Login.class));
finish();
});
}
}
Your code is explicitly asking Firestore for a long (integer) value:您的代码明确要求 Firestore 提供长(整数)值:
documentSnapshot.getLong("Points")
That's not what you want.那不是你想要的。 You want a double (floating point) value instead using getDouble() :
您想要一个双精度(浮点)值,而不是使用getDouble() :
documentSnapshot.getDouble("Points")
You can then turn that into a string for your TextView.然后,您可以将其转换为 TextView 的字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.