@Dao
interface TextDao {
@Query("SELECT * FROM text_table")
fun getAllData() : Flow<List<TextEntity>>
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun insert(text : TextEntity)
@Query("DELETE FROM text_table")
fun deleteAllData()
@Update
fun updateTextItem(text: TextEntity)
@Delete
fun deleteTextItem(text : TextEntity)
}
class MainActivity : AppCompatActivity() {
private lateinit var viewModel: MainViewModel
private val customAdapter : CustomAdapter by lazy {
CustomAdapter(viewModel)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
viewModel = ViewModelProvider(this).get(MainViewModel::class.java)
val insertBtn = findViewById<Button>(R.id.insertBtn)
val rv = findViewById<RecyclerView>(R.id.rv)
insertBtn.setOnClickListener {
// Dialog만들기
val mDialogView = LayoutInflater.from(this).inflate(R.layout.custom_dialog, null)
val mBuilder = AlertDialog.Builder(this)
.setView(mDialogView)
.setTitle("Login Form")
val mAlertDialog = mBuilder.show()
val okButton = mDialogView.findViewById<Button>(R.id.successButton)
val inputText = mDialogView.findViewById<EditText>(R.id.editText)
okButton.setOnClickListener {
viewModel.insert(inputText.text.toString())
mAlertDialog.dismiss()
}
}
viewModel.readAllData.observe(this, Observer {
customAdapter.setData(it as ArrayList<TextEntity>)
})
rv.adapter = customAdapter
rv.layoutManager = LinearLayoutManager(this)
}
}
class CustomAdapter(viewModel: MainViewModel) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
private val TAG = CustomAdapter::class.java.simpleName
private val viewModel = viewModel
private var textList = ArrayList<TextEntity>()
class ViewHolder(view : View) : RecyclerView.ViewHolder(view){
val textView : TextView = view.findViewById(R.id.textArea)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.text_row_item, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.textView.text = textList[position].text
holder.itemView.findViewById<Button>(R.id.remove).setOnClickListener {
viewModel.removeItem(textList[position])
}
holder.itemView.findViewById<Button>(R.id.update).setOnClickListener {
viewModel.updateItem(TextEntity(textList[position].id, "updated"))
}
}
override fun getItemCount(): Int {
return textList.size
}
fun setData(text_list : ArrayList<TextEntity>){
textList = text_list
notifyDataSetChanged()
}
}
class MainViewModel(application: Application) : AndroidViewModel(application) {
var readAllData : LiveData<List<TextEntity>>
val context = getApplication<Application>().applicationContext
val db = TextDatabase.getDatabase(context)
init {
readAllData = db.textDao().getAllData().asLiveData()
}
fun insert(text : String) = viewModelScope.launch(Dispatchers.IO) {
db.textDao().insert(TextEntity(0, text))
}
fun updateItem(text : TextEntity) = viewModelScope.launch(Dispatchers.IO) {
db.textDao().updateTextItem(text)
}
fun removeItem(text : TextEntity) = viewModelScope.launch(Dispatchers.IO) {
db.textDao().deleteTextItem(text)
}
}
'Android Jetpack' 카테고리의 다른 글
ROOM Embeded (0) | 2022.07.22 |
---|---|
ROOM Migration (0) | 2022.07.22 |
ROOM + FLOW CRUD - 3 (0) | 2022.07.12 |
ROOM + FLOW CRUD - 2 (0) | 2022.07.11 |
ROOM + FLOW CRUD - 1 (0) | 2022.07.03 |