How to save ListView content?

how to save contents of a listview to a text file in android?

  • I have a listview that has a bunch of content and I want to know how I can save the contents inside the list view as a text file? I am pulling all the content from a database.

  • Answer:

    I am asuming you want to save the content of the row that was clicked by the user: If using ListActivity override onListItemClick (ListView lv, View v, int position, long id). Then String str = lv.getItemAtPosition(position).toString() can give you the string contained in the row. Other possibilities exist depending on your exact implementation. You also have access to the view that was clicked. I dont think you want to save the content of all the rows as you already have that in your database and can simply query and save from there. Once you have the string. create a new file and write to it. One way of writing to file: try { File f = File.createTempFile("file", ".txt", Environment.getExternalStorageDirectory ()); FileWriter fw = new FileWriter(f); fw.write(str); fw.close(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error while saving file", Toast.LENGTH_LONG).show(); }

Christian at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.