我试图将数据存储在firebase数据库中,而我必须从csv文件中获取数据。我正在使用文件选择器意图选择csv文件。我正在使用CSVReader从csv文件读取数据。
public class CSV_UploadActivity extends AppCompatActivity implements View.OnClickListener {
private Button btnUpload;
public static final int PICKFILE_RESULT_CODE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_csv__upload);
btnUpload = (Button) findViewById(R.id.btnUpload);
btnUpload.setOnClickListener(this);
}
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("text/csv");
startActivityForResult(Intent.createChooser(intent, "Open CSV"), 1);
}
@Override
protected void onActivityResult ( int requestCode, int resultCode, Intent data){
if(resultCode == RESULT_OK) {
onImport(new File(data.getData().getPath()));
}
}
public void onImport(File files) {
try {
CSVReader dataRead = new CSVReader(new FileReader(files));
String[] nextLine;
try {
while ((nextLine = dataRead.readNext()) != null) {
// nextLine[] is an array of values from the line
String AEMAIL = nextLine[0];
String BRANCH = nextLine[1];
String DOB = nextLine[2];
String GENDER = nextLine[3];
String MOBILE_NO = nextLine[4];
String NAME = nextLine[5];
RegStudInfo StudInfo = new RegStudInfo(AEMAIL,BRANCH,DOB,GENDER,MOBILE_NO,NAME);
System.out.println(StudInfo.getBRANCH());
try {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference("users/CE");
String KeyValue = mDatabase.push().getKey();
mDatabase.child(KeyValue).setValue(StudInfo);
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(), "Data inserted into table...", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}