Skip to content

Instantly share code, notes, and snippets.

View manuelernesto's full-sized avatar
🟣

Manuel Ernesto manuelernesto

🟣
View GitHub Profile
private fun apagarPalestrante() {
AlertDialog.Builder(requireContext()).apply {
setTitle("Deseja realmente apagar?")
setNegativeButton("Não") { _, _ ->
}
setPositiveButton("Sim") { _, _ ->
launch {
context.let {
PalestranteDB.getDatabase(it).dao().apagar(mPalestrante!!)
...
<Button
android:id="@+id/room_btn_apagar"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="@string/txt_apagar"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="@+id/room_btn_salvar"
app:layout_constraintStart_toStartOf="@+id/room_btn_salvar"
...
private var mPalestrante: Palestrante? = null
...
...
launch {
context?.let {
if (mPalestrante != null) {
palestrante.id = mPalestrante!!.id
PalestranteDB.getDatabase(it).dao().actualizar(palestrante)
Toast.makeText(it, "Palestrante actualizado!", Toast.LENGTH_SHORT).show()
} else {
PalestranteDB.getDatabase(it).dao().salvar(palestrante)
Toast.makeText(it, "Cadastro com sucesso!", Toast.LENGTH_SHORT).show()
holder.view.setOnClickListener {
val action = HomeFragmentDirections.toSpeakersFragment()
action.palestrante = palestrantes[position]
Navigation.findNavController(it).navigate(action)
}
...
<fragment
android:id="@+id/speakersFragment"
android:name="io.github.manuelernesto.listofspeakers.ui.SpeakersFragment"
android:label="@string/txt_cadastro"
tools:layout="@layout/fragment_speakers">
<action
android:id="@+id/to_homeFragment"
app:destination="@id/homeFragment" />
<argument
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
rv_room.setHasFixedSize(true)
rv_room.layoutManager = LinearLayoutManager(context)
launch {
context?.let {
val palestrantes = PalestranteDB.getDatabase(it).dao().buscarTodos()
rv_room.adapter = PalestranteAdapter(palestrantes)
class PalestranteAdapter(private val palestrantes: List<Palestrante>) :
RecyclerView.Adapter<PalestranteAdapter.PalestranteViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = PalestranteViewHolder(
LayoutInflater.from(parent.context).inflate(R.layout.palestrante_item, parent, false)
)
override fun onBindViewHolder(holder: PalestranteViewHolder, position: Int) {
holder.view.room_item_txt_nome.text = palestrantes[position].palestrante
holder.view.room_item_txt_tecnologia.text = palestrantes[position].tecnologia
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content">
<TextView
android:id="@+id/room_item_txt_nome"
android:layout_width="0dp"
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
room_btn_salvar.setOnClickListener {
val palestrante = Palestrante(
palestrante = room_et_nome.text.toString(),
tecnologia = room_et_tecnologia.text.toString(),
topico = room_et_topico.text.toString()
)