Wiring Up Assets for Use

Now that you have your asset filenames, you should present them to the user. Eventually, you will want the files to be played, so it makes sense to have an object responsible for keeping track of the filename, the name the user should see, and any other information related to that sound.

Create a Sound class to hold all of this. (Remember to let Android Studio generate your getters.)

Listing 20.16  Creating Sound object (Sound.java)

public class Sound {
    private String mAssetPath;
    private String mName;

    public Sound(String assetPath) {
        mAssetPath = assetPath;
        String[] components = assetPath.split("/");
        String filename = components[components.length - 1];
        mName = filename.replace(".wav", "");
    }

    public String getAssetPath() {
        return mAssetPath;
    }

    public String getName() {
        return mName;
    }
}

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<Sound> 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<Sound> 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<SoundHolder> {
    private List<Sound> mSounds;

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

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

And then pass in BeatBox’s sounds in onCreateView(…).

Listing 20.19  Passing in Sounds to adapter (BeatBoxFragment.java)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    FragmentBeatBoxBinding binding = DataBindingUtil
            .inflate(inflater, R.layout.fragment_beat_box, container, false);

    binding.recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
    binding.recyclerView.setAdapter(new SoundAdapter());
    binding.recyclerView.setAdapter(new SoundAdapter(mBeatBox.getSounds()));

    return binding.getRoot();
}

With that, you should see a grid of buttons when you run BeatBox (Figure 20.6).

Figure 20.6  Empty buttons

Screenshot shows BeatBox app screen in Android. The Blank Buttons are arranged in rows and columns.

To populate the buttons with titles, you will use some additional tools from your new data binding utility belt.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
44.197.238.222