从回收者视图中选择用户数据后,将其加载到新活动中

时间:2019-06-12 00:23:51

标签: firebase kotlin android-recyclerview

我的应用程序当前显示一个回收视图,其中包含使用Firebase在系统中注册的用户的名称。 现在,我需要使该回收视图可单击,单击用户名,然后打开一个显示其一些数据的新活动,例如名称。

class PacienteAdapter(internal var context: Context) : RecyclerView.Adapter<PacienteAdapter.ViewHolder>() {

    internal var userList: MutableList<PacienteRecycle>

    val lastItemId: String?
        get() = userList[userList.size - 1].id

    fun addAll(newUsers: List<PacienteRecycle>) {
        val init = userList.size
        userList.addAll(newUsers)
        notifyItemRangeChanged(init, newUsers.size)
    }

    init {
        this.userList = ArrayList()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val itemView = LayoutInflater.from(context).inflate(R.layout.paciente_item, parent, false)
        return ViewHolder(itemView)
    }

    //context
    override fun getItemCount(): Int {
        return userList.size
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

        holder.textViewName.text = userList[position].nome
        holder.itemView.setOnClickListener {

        }
    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        internal var textViewName = itemView.findViewById(R.id.nome) as TextView

    }

}

1 个答案:

答案 0 :(得分:4)

要使此回收视图可点击并在单击用户名称时打开一个新活动并显示其中的一些数据,可以使用Intent,在名称文本视图上实现onClickListener并将数据放入其中,如下所示:

class PacienteAdapter(internal var context: Context) : RecyclerView.Adapter<PacienteAdapter.ViewHolder>() {

internal var userList: MutableList<PacienteRecycle>

val lastItemId: String?
    get() = userList[userList.size - 1].id

fun addAll(newUsers: List<PacienteRecycle>) {
    val init = userList.size
    userList.addAll(newUsers)
    notifyItemRangeChanged(init, newUsers.size)
}

init {
    this.userList = ArrayList()
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
    val itemView = LayoutInflater.from(context).inflate(R.layout.paciente_item, parent, false)
    return ViewHolder(itemView)
}

//context
override fun getItemCount(): Int {
    return userList.size
}

override fun onBindViewHolder(holder: ViewHolder, position: Int) {

    holder.textViewName.text = userList[position].nome
    holder.textViewName.setOnClickListener {
        val intent = Intent()
            intent.setClass(context, YourActivity::class.java)

            //pass your data to the Activity
            intent.putExtra("yourDataKey1", "your-data1")
            intent.putExtra("yourDataKey2", "your-data2")
            intent.putExtra("clickedUserName", userList[position].nome)

            context.startActivity(intent)
    }
    holder.itemView.setOnClickListener {
        //put the same code here to make the whole item in the view clickable
    }
}


inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    internal var textViewName = itemView.findViewById(R.id.nome) as TextView 
} }

然后通过这样的Intent在其他活动中捕获数据:

class YourActivity : Activity() {

//data
private var receivedData1: String? = ""
private var receivedData2: String? = ""
private var clickedUserName String? = ""

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_single_pocmaps)

    receivedData1 = intent.extras?.getDouble("yourDataKey1")
    receivedData2 = intent.extras?.getDouble("yourDataKey2")
    clickedUserName = intent.extras?.getString("clickedUserName")
}  }