首页 > 解决方案 > 我可以将稀疏数组转换为字符串吗?

问题描述

我正在开发 QR 码扫描应用程序。我正在使用 Barcode Detector 读取 QR 码,结果存储在 SparseArray 中。所以我的问题是我可以将 SparseArray 转换为 String 吗?这样我就可以使用拆分函数打破字符串,然后将其与另一个字符串进行比较。

@Override
        public void receiveDetections(Detector.Detections<Barcode> detections) {
            final SparseArray<Barcode> qrcodes = detections.getDetectedItems();


            if (qrcodes.size() != 0) {
                txtResult.post(new Runnable() {
                    @Override
                    public void run() {
                        Vibrator vibrator = (Vibrator) getApplicationContext().getSystemService(Context.VIBRATOR_SERVICE);
                        vibrator.vibrate(100);

                        txtResult.setText(qrcodes.valueAt(0).displayValue);


                    }
                });
            }

        }

标签: javaandroidqr-codebarcode

解决方案


您可以使用toString()以下方法:SparseArray<Int>().toString(). 如果您正在处理内置类(库类),请扩展该类以自定义您想要的任何行为,例如:

class b<T> : SparseArray<T>(){

    @Override
    public String toString() {
      return //customised String;
 }
}

在此之后,您可以调用SparseArray<Int>().toString()以获取您的自定义字符串表示。


推荐阅读