Android: how to store input from edittext to .csv

Pretty new to android, I come from a heavy fortran background. I am trying to make applications successful so far.

I could not find a way: save the "edittext" field using the (save) button, and then save this data entered by the user into a CSV file (preferably in the internal storage).

I found a lot of articles, but everyone looks back at the fundamental part that I want (above).

the best idea i got is to generate .csv in the class and then create a method to save the "edittext" as a new line and then to output that line to .csv

Hope this can just be explained, I just can't find this simple explanation anywhere, or at least that I can understand ...

+4
source share
1 answer

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 to the manifest file

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
0
source

All Articles