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 androidx.lifecycle.ViewModelProvider;
import com.majinnaibu.monstercards.R; import com.majinnaibu.monstercards.R;
import com.majinnaibu.monstercards.models.Monster;
public class MonsterFragment extends Fragment { public class MonsterFragment extends Fragment {
@@ -20,15 +21,23 @@ public class MonsterFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { 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); monsterViewModel = new ViewModelProvider(this).get(MonsterViewModel.class);
View root = inflater.inflate(R.layout.fragment_monster, container, false); View root = inflater.inflate(R.layout.fragment_monster, container, false);
final TextView textView = root.findViewById(R.id.text_monster); monsterViewModel.setMonster(monster);
monsterViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
final TextView monsterName = root.findViewById(R.id.name);
monsterViewModel.getName().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override @Override
public void onChanged(@Nullable String s) { public void onChanged(@Nullable String name) {
textView.setText(s); monsterName.setText(name);
} }
}); });
return root; return root;
} }
} }

View File

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

View File

@@ -1,22 +1,30 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" <ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".ui.monster.MonsterFragment"> tools:context=".ui.monster.MonsterFragment">
<TextView <androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/text_monster"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:textAlignment="center" android:fontFamily="sans-serif"
android:textSize="20sp" android:textColor="#9B2818"
app:layout_constraintBottom_toBottomOf="parent" android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent"
</androidx.constraintlayout.widget.ConstraintLayout> tools:text="Pixie" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>