0

I have a static ArrayList<String> items which is shown via listview after it gets populated with elements.

What is the easiest way to to save the arraylist and/or listview to a textfile?

nanofarad
  • 40,330
  • 4
  • 86
  • 117
Jack Smith
  • 101
  • 1
  • 1
  • 8
  • 1
    I think, you should check [this](http://stackoverflow.com/questions/16111496/java-how-can-i-write-my-arraylist-to-a-file-and-read-load-that-file-to-the) answer. – 306 Apr 27 '16 at 14:53
  • hm i tried it tbh but to no avail. maybe its because i used an arrayadapter. https://gyazo.com/ace5e232e9cebe53d3d8f236941d1842 – Jack Smith Apr 27 '16 at 14:58

2 Answers2

0

I've done it this way. But the body here is JSON String.

public void saveTextFile(Context context, String sFileName, String sBody) {
    try {
        String root = Environment.getExternalStorageDirectory().getAbsolutePath();
        File filePath = new File(root + "/Personal Encyclopedia/Data" + File.separator);
        if (!filePath.exists()) {
            filePath.mkdirs();
        }
        File writeFile = new File(filePath, sFileName);
        FileWriter writer = new FileWriter(writeFile, true);
        writer.append(sBody);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Make your ArrayList a string (possible by overriding toString() method) and call with file name YourFileName.txt. May be it is helpful

Waqas Ahmed Ansari
  • 1,683
  • 15
  • 30
  • thx for ur answers guys.. i sort of did it but the output is a string and not a listview (which i guess is difficult to do). Used different code : https://gyazo.com/bfd329d0ba9d43040935d964a226d42c – Jack Smith Apr 27 '16 at 15:44
0

You may take a look at the FileWriter.

mrousavy
  • 311
  • 1
  • 8