Adds monster name to monster cards.
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -1,22 +1,30 @@
|
||||
<?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:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".ui.monster.MonsterFragment">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/text_monster"
|
||||
<androidx.constraintlayout.widget.ConstraintLayout
|
||||
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_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:textAlignment="center"
|
||||
android:textSize="20sp"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
android:fontFamily="sans-serif"
|
||||
android:textColor="#9B2818"
|
||||
android:textSize="34sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
tools:text="Pixie" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
</ScrollView>
|
||||
Reference in New Issue
Block a user