Reputation: 95
I'm currently developing an app that records sounds and saves them as *.mp3 files. Once a user is finished recording a sound, he is asked to rename it. After the user is done renaming it, the file is saved to a certain location on the phone as whateverTheUserTypedHere.mp3.
What I currently implemented in the app:
Once the user sets the file name, the name of the file is taken to the second activity (variable called 'filename'). This is the code for making the listview:
//global variables
ListView listView;
ArrayAdapter<String> listAdapter;
ArrayList<String> fileNames = new ArrayList<String>();
//this is inside onCreate
listView = (ListView) findViewById (R.id.mainListView);
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, fileNames);
//this is inside the method that is called everytime file is recorded
public void setFileName(final Editable filename) {
Log.d("2", "Set filename from first activity " + filename);
fileNames.add(filename.toString());
listView.setAdapter(listAdapter);
So this all works as it should now. Everytime a file is recorded and renamed, it is instantly added to listView in a second activity. But this code has a huge flaw - listview's state isn't saved, so everytime I close the app and re-open it, the recorded files are still there on the phone's storage, but listView is nowhere to be found. So what I would need to do is to implement something that will keep saving listview's state everytime it is changed.
What I'm thinking I could do:
I was thinking an easier way would be something else. Instead of adding elements to the listView while recording and then saving listview's state, I could basically build a new listView everytime the activity is opened. I could implement a method that would read a specific directory on my phone and only read *.mp3 files. So that everytime the listview activity would be opened, the listView would be automatically "generated" based on files that are in a specific directory.
What I'm asking for:
I'm new to Android programming, I've been only doing this for 2-3 months. I'd appreciate it a lot if people could give me some pointers on what should I do. Should I keep my currently implemented method that adds listView rows right after file was recorded? If so, how would I then save it's state/how would I save it's added rows? Or would it be easier to implement a method that reads files from a certain directory and then generate a listview each time activity is opened? If so, I would really appreciate some pointers on how to do that, since I've never done anything like that before.
Thank you!
Upvotes: 0
Views: 93
Reputation: 234795
You need to keep your data in some persistent form. You should then retrieve (or start its retrieval) it inside the second activity's onCreate
method. This can be handled in several ways. For instance, you could simply read each file name from the folder in which you are storing the .mp3 files, construct an array, and use an ArrayAdapter
as you are currently doing. The only difference would be that you would be reading the data each time the activity starts. (This is necessary to correctly deal with the activity life cycle, where an activity can be destroyed and re-created when, say, the user simply reorients the phone.)
Rather than reading the file system directly, a better way might be to use a data base or one of the other persistent storage mechanisms available in Android to maintain a list of file names. (See the guide topic Storage Options as well as the tutorial Saving Data.) I would recommend using an SQLite data base. It's a bit of work to set up, but it provides a lot of flexibility and Android provides nice classes to help bring data from a data base to the screen. If you make your activity a ListActivity
, for instance, you can simply issue a query for the data in onCreate
, tell the activity to manage the resulting cursor, wrap a SimpleCursorAdapter
around the result, and tell the activity to use the adapter. The system will then automatically take care of properly handling all the activity life cycle events.
There's lots of material available to show you how to do this. In addition to the above links, there are several sample projects (that the above links will refer you to), as well as several good third-party tutorials (such as this one) on the web.
Upvotes: 0
Reputation: 1678
If you have to scan all .mp3 files on the entire storage each time your app is opened, it will take quite some time. Even if it is limited just to a specific folder, it is better to store a file yourself that contains the listview. This file must be stored within the boundaries of your own app and it must always be stored at the same position. If it does not exist, then you know the user opened your application for the very first time.
If you want a quick guide on the java BufferedWriter, you can find one here: http://www.mkyong.com/java/how-to-write-to-file-in-java-bufferedwriter-example/
Upvotes: 1