Fairly new to android, I come from a heavy fortran background. I've been trying to make apps, sucessful until now.
I'm having trouble finding a way for: saving an 'edittext' field by use of a button(save), then saving this user inputted data to a .csv file(preferably in internal storage).
I've found many articles but everyone glazes over the fundemental part I want(above).
the best idea I've got, is of generating the .csv in the class, then creating a method to save the 'edittext' as a new string, then to output that string to the .csv
Hopefully this can be simply explained, I just cant find this simple explanation anywhere, or at-least that I can understand...
Please try this.I hope this code helps you.
CSVFileWriter.java
public class CSVFileWriter {
private PrintWriter csvWriter;
private File file;
public CSVFileWriter(File file) {
this.file = file;
}
public void writeHeader(String data) {
try {
if (data != null) {
csvWriter = new PrintWriter(new FileWriter(file, true));
csvWriter.print(",");
csvWriter.print(data);
csvWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
SampleActivity.java
public class SampleActivity extends Activity {
CSVFileWriter csv;
StringBuffer filePath;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
saveButton = (Button) findViewById(R.id.button1);
editText = (EditText) findViewById(R.id.editText1);
filePath = new StringBuffer();
filePath.append("/sdcard/abc.csv");
file = new File(filePath.toString());
csv = new CSVFileWriter(file);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
csv.writeHeader(editText.getText().toString());
}
});
}
}
Add this in manifest file
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With