5

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...

Mr.Sandy
  • 4,299
  • 3
  • 31
  • 54
shaolinalex1
  • 51
  • 1
  • 2
  • 1
    http://stackoverflow.com/a/4632617/1168654 and http://stackoverflow.com/q/5989279/1168654 – Dhaval Parmar Apr 12 '13 at 04:48
  • I used this source to create the .csv http://www.mkyong.com/java/how-to-export-data-to-csv-file-java/ then this to bundle the data to .csv http://stackoverflow.com/questions/9645211/how-to-bundle-the-data-captured-from-customized-dialogedittext-datepicker-spinn uut i keep getting lost how use the save button to save that edittext data to be output to .csv – shaolinalex1 Apr 12 '13 at 04:51

1 Answers1

1

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" />
swetha kini
  • 564
  • 1
  • 6
  • 23
  • You should write your package name on the top of CSVFileWriter.java first line should be seen like: package com.new.app; – Bay Jun 14 '19 at 05:59