如何从Kotlin的RecycleView适配器获取SQLite ID?

时间:2018-09-16 01:30:19

标签: sqlite android-studio android-recyclerview crud

对不起,我的英语不好。我是Android Studio的初学者。

我使用自定义RecycleView在Android Studio中构建相同的CREATE,UPDATE和DELETE应用程序。但是,我困惑当单击recycleview项时如何获取行数据。

我可以在RecycleView项目中获得可见文本吗?或者我可以直接从SQLite获取数据。

如何从此适配器获取SQLite ID?

package gh.alwathan.sipinsyar

import android.support.v7.widget.PopupMenu
import android.support.v7.widget.RecyclerView
import android.view.*
import android.widget.TextView
import android.widget.Toast

class NasabahAdapter(val userList: ArrayList<Nasabah>) : RecyclerView.Adapter<NasabahAdapter.ViewHolder>() {



    //this method is returning the view for each item in the list
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NasabahAdapter.ViewHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.list_item_nasabah, parent, false)
        return ViewHolder(v)

    }

    //this method is binding the data on the list
    override fun onBindViewHolder(holder: NasabahAdapter.ViewHolder, position: Int) {
        holder.bindItems(userList[position])
        holder.itemView.setOnLongClickListener {

            //holder.nametxt.setText(players.get(position).getName());

            //creating a popup menu
            val popup = PopupMenu(holder.itemView.getContext(), holder.itemView, Gravity.RIGHT)
            //inflating menu from xml resource
            popup.inflate(R.menu.nasabah_item)
            //adding click listener

            popup.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item: MenuItem? ->

                when (item!!.itemId) {
                    R.id.nasabah_id-> {

                        // GET SQLITE ID FROM HERE

                    }
                }

                true
            })

            //displaying the popup
            popup.show()

            true

        }
    }

    //this method is giving the size of the list
    override fun getItemCount(): Int {
        return userList.size
    }

    //the class is hodling the list view
    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bindItems(user: Nasabah) {
            val textViewName = itemView.findViewById(R.id.nasabah_id) as TextView
            val textViewLastName  = itemView.findViewById(R.id.nasabah_nama) as TextView
            textViewName.text = user.id
            textViewLastName.text = user.nama
        }
    }
}

data class Nasabah(val id: String, val nama: String)

0 个答案:

没有答案