
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : AppCompatActivity() { | |
private val viewModel : MainViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
// Create | |
val createBtn = findViewById<Button>(R.id.create) | |
createBtn.setOnClickListener { | |
val ranNumber = (0..100).random().toString() | |
val userEntity = NumberEntity(0, ranNumber) | |
viewModel.create(userEntity) | |
} | |
viewModel.read() | |
viewModel.userEntityList.observe(this, Observer { | |
Log.d("Main", it.toString()) | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainViewModel : ViewModel() { | |
private val repository = Repository() | |
lateinit var userEntityList : LiveData<List<NumberEntity>> | |
fun create(userEntity: NumberEntity) = viewModelScope.launch(Dispatchers.IO){ | |
repository.create(userEntity) | |
} | |
fun read() { | |
userEntityList = repository.read().asLiveData() | |
} | |
fun update(){ | |
} | |
fun delete(){ | |
} | |
} |
'Android Jetpack' 카테고리의 다른 글
Room Advanced - 6 (Update / Delete) (0) | 2023.02.02 |
---|---|
Room Advanced - 5 (Adapter) (0) | 2023.02.02 |
Room Advanced - 3 (구조) (0) | 2023.02.02 |
Room Advanced - 2 (DB) (0) | 2023.02.02 |
Room Advanced - 1 (XML) (0) | 2023.02.02 |