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
Related Q & A:
- How do I apply the overlay to draw a line in maps Android?Best solution by Stack Overflow
- How to get a Coordinates of a 3D Object in Android?Best solution by Game Development
- How to refresh a listview within a Fragment?Best solution by Stack Overflow
- How to delete a line from a .txt file in node.js?Best solution by c-sharpcorner.com
- How to search for a particular string in a text file using java?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.