0

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!

Pizzret
  • 95
  • 1
  • 11
  • Is the problem simply preserving the data during activity life cycle changes (e.g., restarting the activity due to reorienting the phone, etc.)? Or is the problem how to persist the data so that it's available when the activity is launched anew? – Ted Hopp Aug 07 '13 at 21:53
  • @TedHopp The second problem; how to persist the dara when activity is completely destroyed and re-launched – Pizzret Aug 07 '13 at 22:23

2 Answers2

1

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/

bas
  • 1,678
  • 12
  • 23
  • So if I get this right; I'll be storing array to this file everytime I record a new file and then just call listview in onCreate to read array data from this file? – Pizzret Aug 07 '13 at 21:47
  • That is one way to do it. There are several ways to store the data to the file, I suggest reading up on some tutorials. http://docs.oracle.com/javase/tutorial/essential/io/file.html. But basically, yes, you will write the file each time you save a .mp3 and you will read it when starting the app. It would be most efficiënt to append a line when inserting a new .mp3 rather than rewriting the entire file. – bas Aug 07 '13 at 21:52
  • Yes! Works perfectly! I followed the tutorial you gave me and then read the file to arraylist. Now I only need to figure out how to delete a certain line in the .txt file. Thank you – Pizzret Aug 08 '13 at 12:53
0

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.

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521