Creating a ViewModel
Creating a ViewModel
Let’s create your view model. Create a new class called SoundViewModel and give it two properties: a
Sound for it to use and a BeatBox to (eventually) play that sound with.
Listing 20.20 Creating SoundViewModel (SoundViewModel.java)
public class SoundViewModel {
private Sound mSound;
private BeatBox mBeatBox;
public SoundViewModel(BeatBox beatBox) {
mBeatBox = beatBox;
}
public Sound getSound() {
return mSound;
}
public void setSound(Sound sound) {
mSound = sound;
}
}
These properties are the interface your adapter will use. For the layout file, you will want an additional
method to get the title that the button should display. Add it now to SoundViewModel.
Listing 20.21 Adding binding methods (SoundViewModel.java)
public class SoundViewModel {
private Sound mSound;
private BeatBox mBeatBox;
public SoundViewModel(BeatBox beatBox) {
mBeatBox = beatBox;
}
public String getTitle() {
return mSound.getName();
}
public Sound getSound() {
return mSound;