From e3266de95bd8329087e3f2d0a6e825825164d8e5 Mon Sep 17 00:00:00 2001 From: Yannine Alvarez Date: Tue, 19 May 2020 20:07:41 -0400 Subject: [PATCH] Delete en recycler view. --- app/src/main/java/com/example/ayudapy/Dao.kt | 2 ++ app/src/main/java/com/example/ayudapy/ListaModel.kt | 3 +++ app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt | 26 ++++++++++++++++++++++++++ app/src/main/java/com/example/ayudapy/PendienteLista.kt | 6 ++++++ app/src/main/java/com/example/ayudapy/Repository.kt | 4 ++++ app/src/main/res/layout/pendientes_items.xml | 2 ++ app/src/main/res/values/strings.xml | 1 + 7 files changed, 44 insertions(+) diff --git a/app/src/main/java/com/example/ayudapy/Dao.kt b/app/src/main/java/com/example/ayudapy/Dao.kt index 9e9a582..56b6a71 100644 --- a/app/src/main/java/com/example/ayudapy/Dao.kt +++ b/app/src/main/java/com/example/ayudapy/Dao.kt @@ -15,6 +15,8 @@ interface DaoPedido { @Delete fun delete(pedido: PedidoSave) + + } /* * @Dao diff --git a/app/src/main/java/com/example/ayudapy/ListaModel.kt b/app/src/main/java/com/example/ayudapy/ListaModel.kt index 8fd4e24..1bf1d31 100644 --- a/app/src/main/java/com/example/ayudapy/ListaModel.kt +++ b/app/src/main/java/com/example/ayudapy/ListaModel.kt @@ -12,4 +12,7 @@ class PedidoViewModel(application:Application): AndroidViewModel(application){ fun insert(student:PedidoSave){ db.daoPedido().insert(student) } + fun delete(student:PedidoSave){ + db.daoPedido().delete(student) + } } \ No newline at end of file diff --git a/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt b/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt index ddc3178..59a7675 100644 --- a/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt +++ b/app/src/main/java/com/example/ayudapy/ListaPedidoAdapter.kt @@ -1,11 +1,13 @@ package com.example.ayudapy import android.content.Context +import android.graphics.ColorSpace import android.os.Build import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import android.widget.Toast import androidx.annotation.RequiresApi import androidx.recyclerview.widget.RecyclerView import kotlinx.android.synthetic.main.pendientes_items.view.* @@ -14,9 +16,14 @@ import java.time.LocalDateTime import java.time.format.DateTimeFormatter import java.time.format.FormatStyle + + class RecyclerViewAdapter(val pedido: List) : RecyclerView.Adapter(){ + lateinit var context: Context + private lateinit var db:pedidoDataBase + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) : RecyclerViewAdapter.ViewHolder { val v: View = LayoutInflater.from(parent.context) @@ -49,8 +56,22 @@ class RecyclerViewAdapter(val pedido: List) holder.date_month.text = mes.toUpperCase() holder.title.text = pedido[position].title holder.message_view.text = pedido[position].message + + holder.delete_layout.setOnClickListener { + //Toast.makeText(context,"clicked",Toast.LENGTH_SHORT).show() + println("Hace clic ") + // val pedido = Thread{ + db.daoPedido().delete(pedido[position]) + // } + // pedido.start() + + } + + + } + override fun getItemCount(): Int { return pedido.size } @@ -63,11 +84,16 @@ class RecyclerViewAdapter(val pedido: List) return super.getItemViewType(position) } + class ViewHolder(itemView:View): RecyclerView.ViewHolder(itemView){ val date_day = itemView.date_day val date_month = itemView.date_month val title = itemView.title val message_view = itemView.message_view + val delete_layout = itemView.delete_layout + + + } } diff --git a/app/src/main/java/com/example/ayudapy/PendienteLista.kt b/app/src/main/java/com/example/ayudapy/PendienteLista.kt index 494fc38..d5fa789 100644 --- a/app/src/main/java/com/example/ayudapy/PendienteLista.kt +++ b/app/src/main/java/com/example/ayudapy/PendienteLista.kt @@ -2,16 +2,21 @@ package com.example.ayudapy import android.content.Intent import android.os.Bundle +import android.view.View +import android.widget.ListAdapter +import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView +import kotlinx.android.synthetic.main.pendientes_items.view.* import kotlinx.android.synthetic.main.pendientes_lista.* class PendienteLista : AppCompatActivity() { private lateinit var model: PedidoViewModel + private lateinit var mDb:pedidoDataBase override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.pendientes_lista) @@ -31,6 +36,7 @@ class PendienteLista : AppCompatActivity() { recycler_view.adapter = RecyclerViewAdapter(pedido) }) + } } diff --git a/app/src/main/java/com/example/ayudapy/Repository.kt b/app/src/main/java/com/example/ayudapy/Repository.kt index cbf9963..3c03f89 100644 --- a/app/src/main/java/com/example/ayudapy/Repository.kt +++ b/app/src/main/java/com/example/ayudapy/Repository.kt @@ -16,6 +16,10 @@ class PedidoRepository(private val pedidoDao: DaoPedido) { pedidoDao.insert(word) } + @WorkerThread + fun delete(word: PedidoSave) { + pedidoDao.delete(word) + } /* --------------- BORRAR TODOS LOS DATOS -------------- */ diff --git a/app/src/main/res/layout/pendientes_items.xml b/app/src/main/res/layout/pendientes_items.xml index 8ce7706..c626920 100644 --- a/app/src/main/res/layout/pendientes_items.xml +++ b/app/src/main/res/layout/pendientes_items.xml @@ -20,10 +20,12 @@ android:foreground="?android:attr/selectableItemBackground"> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4b43f4d..f3c84d8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,4 +4,5 @@ Hello blank fragment + Eliminar -- libgit2 0.26.0