简体   繁体   English

如何从firestore数据库显示十进制值到android studio

[英]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.

相关问题 如何在android studio中显示firestore数据库中navigation header中的用户数据 - How to display the user data in navigation header from firestore database in android studio ANDROID-如何在android studio中将数据库mysql php的值显示为textview - ANDROID - How to display value of database mysql php into textview in android studio 如何将字符串值设置为从 android 中的 Firestore 数据库中检索到的微调器? - How to set string value to spinner retrieved from Firestore database in android? Android Studio,如何从Sqlite数据库中检索数据并将其显示到textview中? - Android Studio, how to retrieve data from Sqlite database and display it into textview? 如何使用 Android 在 RecyclerView 中显示来自 Firestore 的数据? - How to display data from Firestore in a RecyclerView with Android? 如何在 Firestore 数据库(Android Studio)中将所有记录从一个位置移动到另一个位置 - How to move all records from one location to another in Firestore Database (Android Studio) 如何从 sqlite 数据库(android studio java)中获取 ID 值 - how to get ID Value from sqlite database (android studio java) 如何将 Double 值格式化为 2 个小数位? - Java (Android Studio) - How to Format a Double value to 2 Decimal places ? - Java (Android Studio) 将数据从 Firestore 数据库(不是实时数据库)移动到 Android Studio 中的新集合 - Move data from Firestore Database (not Realtime Database) to new collection in Android Studio 如何从android的fireStore数据库中读取嵌套地图 - How to read the nested maps from fireStore database from android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM