Adds monster name to monster cards.

This commit is contained in:
2020-09-01 00:30:12 -07:00
parent 401839edde
commit cd96abfcdc
4 changed files with 63 additions and 24 deletions

View File

@@ -0,0 +1,13 @@
package com.majinnaibu.monstercards.models;
public class Monster {
private String mName;
public String getName() {
return mName;
}
public void setName(String value) {
mName = value;
}
}

View File

@@ -13,6 +13,7 @@ import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.majinnaibu.monstercards.R;
import com.majinnaibu.monstercards.models.Monster;
public class MonsterFragment extends Fragment {
@@ -20,15 +21,23 @@ public class MonsterFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// TODO: remove this block make the monster ID a parameter to the view and get the monster from saved data (sqlite)
Monster monster = new Monster();
monster.setName("Pixie");
// END remove block
monsterViewModel = new ViewModelProvider(this).get(MonsterViewModel.class);
View root = inflater.inflate(R.layout.fragment_monster, container, false);
final TextView textView = root.findViewById(R.id.text_monster);
monsterViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
monsterViewModel.setMonster(monster);
final TextView monsterName = root.findViewById(R.id.name);
monsterViewModel.getName().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
public void onChanged(@Nullable String name) {
monsterName.setText(name);
}
});
return root;
}
}

View File

@@ -4,16 +4,25 @@ import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import com.majinnaibu.monstercards.models.Monster;
public class MonsterViewModel extends ViewModel {
private MutableLiveData<String> mText;
public MonsterViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is monster fragment");
mMonster = null;
mName = new MutableLiveData<>();
mName.setValue("");
}
public LiveData<String> getText() {
return mText;
private MutableLiveData<String> mName;
public LiveData<String> getName() {
return mName;
}
private Monster mMonster;
public void setMonster(Monster monster) {
mMonster = monster;
mName.setValue(mMonster.getName());
}
}