Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1
Wiring Up Assets for Use

In the constructor, you do a little work to make a presentable name for your sound. First,
you split off the filename using String.split(String). Once you have done that, you use
String.replace(String, String) to strip off the file extension, too.


Next, build up a list of Sounds in BeatBox.loadSounds().


Listing 20.17  Creating Sounds (BeatBox.java)


public class BeatBox {
...
private AssetManager mAssets;
private List mSounds = new ArrayList<>();


public BeatBox(Context context) {
...
}


private void loadSounds() {
String[] soundNames;
try {
...
} catch (IOException ioe) {
...
}


for (String filename : soundNames) {
String assetPath = SOUNDS_FOLDER + "/" + filename;
Sound sound = new Sound(assetPath);
mSounds.add(sound);
}
}


public List getSounds() {
return mSounds;
}
}


Then wire up SoundAdapter to a List of Sounds.


Listing 20.18  Hooking up to Sound list (BeatBoxFragment.java)


private class SoundAdapter extends RecyclerView.Adapter {
private List mSounds;


public SoundAdapter(List sounds) {
mSounds = sounds;
}
...
@Override
public void onBindViewHolder(SoundHolder soundHolder, int position) {
}


@Override
public int getItemCount() {
return 0;
return mSounds.size();
}
}

Free download pdf