在Android的CSV中打印Firebase数据集

问题描述 投票:0回答:1

我正在开发一个android应用程序,用于扫描附近的蓝牙设备并获取这些设备的详细信息。详细信息表示设备名称,设备地址和类似的RSSI。然后,我将这些数据发送到Firebase实时数据库。下面我提到了我的数据库结构。

enter image description here

我想检索特定的数据集并将这些数据加载到csv文件中的表中在这里,我提到了如何获取数据集

protected void onStart() {
    super.onStart();

    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("BluetoothDevicesInfo");

    reference.orderByChild("deviceId").equalTo("C6:89:AF:D6:7B:83").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot deviceInf: dataSnapshot.getChildren()){
                String deviceId= deviceInf.child("deviceId").getValue().toString();
                Map<String, String> value = (Map<String, String>) dataSnapshot.getValue();
                Log.i("dataSnapshot", "dataSnapshot : " + new JSONObject(value));
            }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

输出

{"-M8Bu8znHpC4EAstfPLf": 
{"rssi":-92,"distance":1,"deviceId":"C6:89:AF:D6:7B:83","timestamp":"25-05-202023:06:52"}, 
 "-M8Bu8zOuBZjgkkSUcnG": 
{"rssi":-92,"distance":1,"deviceId":"C6:89:AF:D6:7B:83","timestamp":"25-05-2020 23:06:52"}}

如何在CSV上打印此内容

android json csv firebase-realtime-database opencsv
1个回答
0
投票

将这两个添加到各自的位置。

Doc

maven { url 'https://jitpack.io' }

implementation 'com.github.hsmnzaydn:easy-csv:1.0.0'

声明一些全局变量

List<String> headerList = new ArrayList<>();
List<String> dataList = new ArrayList<>();
Snapobj snap ;
List<Snapobj > snapList = new ArrayList<Snapobj>();
public final int WRITE_PERMISSON_REQUEST_CODE =1;

Inside onCreate

 EasyCsv easyCsv = new EasyCsv(MainActivity.this);
 easyCsv.setSeparatorColumn("^");
 easyCsv.setSeperatorLine("~");
 headerList.add("rssi^distance^deviceId^timestamp~");

创建Snapobj类

public class Snapobj {
String rssi;
String distance;
String deviceId;
String timestamp;

public Snapobj(String rssi, String distance, String deviceId, String timestamp) {
    this.rssi = rssi;
    this.distance = distance;
    this.deviceId = deviceId;
    this.timestamp = timestamp;
}
  //Add Getters and Setters
}

现在在onDataChange内部,我们将填充snapList

public void onDataChange(DataSnapshot dataSnapshot) {
  for(DataSnapshot deviceInf: dataSnapshot.getChildren()){
      snapList.add(new Snapobj("pass rssi","pass distance","deviceId","timestamp"));
    }
}

现在,我们需要为数据列表数组构造字符串。创建一个函数并将这些行放进去

for(Snapobj s : snapList)
{
  String s = s.getRssi()+"^"+s.getDistance()+"^"+s.getDeviceId()+"^"+s.getTimestamp()+"~";
  dataList.add(s);
}

最终创建csv文件。

easyCsv.createCsvFile("MyFile", headerList, dataList WRITE_PERMISSON_REQUEST_CODE, new 
FileCallback() {
@Override
public void onSuccess(File file) {
Log.d(TAG, "File saved in phone storage");
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(MainActivity.this,"com.example.mafiacsv.fileprovider",file));
sendIntent.setType("text/*");
sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent shareIntent = Intent.createChooser(sendIntent, null);
startActivity(shareIntent);
}

@Override
public void onFail(String err) {
}
});

将其放在ManifestFile的应用程序标记内

    <provider
        android:name=".GenericFileProvider"
        android:authorities="com.example.mafiacsv.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

创建一个仅扩展FileProvider的空类。

  import androidx.core.content.FileProvider;
  public class GenericFileProvider extends FileProvider {}

onSuccess在成功创建文件并将其保存在手机的内部存储器中时调用。

Important

请确保rssi,距离,id和时间戳的值不包含'^'和'〜',否则它将无法正常工作。我们需要两个唯一的符号来告诉库何时中断行和何时分隔列。

不要忘记询问WRITE_EXTERNAL_STORAGE许可。

© www.soinside.com 2019 - 2024. All rights reserved.