简体   繁体   English

媒体播放器 onclick 片段监听器

[英]media player onclick listener on fragment

I am trying to configure an app to play sounds corresponding to word in a recyclerview on a fragment, however I am having trouble creating the onclick part of it.我正在尝试配置一个应用程序以在片段的 recyclerview 中播放与单词相对应的声音,但是我在创建它的 onclick 部分时遇到了问题。 I get the below error messages on the Adapter: " None of the following functions can be called with the arguments supplied. create(Context,. Uri.) defined in android,media.MediaPlayer create(Context., Int) defined in android.media.MediaPlayer"我在适配器上收到以下错误消息:“无法使用提供的 arguments 调用以下任何函数。在 android 中定义的 create(Context,.Uri.),在 android.media.MediaPlayer create(Context., Int) 中定义的。媒体.MediaPlayer"

"
Unresolved reference: context "


My code is below

vocabularyAdapter
=================


class vocabularyAdapter(val sourceWord: Array<String>, val targetWord: Array<String> ) : RecyclerView.Adapter<vocabularyAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvSourceWord = itemView.findViewById<TextView>(R.id.inEnglishOrFrench)
        val tvLingala = itemView.findViewById<TextView>(R.id.inLingala)


    }

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

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.tvSourceWord.text = sourceWord[position].toString()
        holder.tvLingala.text = targetWord[position]
        holder.itemView.setOnClickListener {
            val mediaPlayer = MediaPlayer.create(holder.context, targetWord[position])
            mediaPlayer.start()


        }


    }

    override fun getItemCount(): Int {
        return sourceWord.size


    }

}

VocabularyFragment
========

class VocabularyFragment : Fragment() {
    // TODO: Rename and change types of parameters
    private var param1: String? = null
    private var param2: String? = null

    private lateinit var adapter: vocabularyAdapter
    private lateinit var recyclerView: RecyclerView
    private lateinit var vocabularyArraylist: ArrayList<Vocabulary>

    lateinit var sourceWordid: Array<String>
    lateinit var lingalaid: Array<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_vocabulary, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        dataInitialize()
        val layoutManager = LinearLayoutManager(context)
        recyclerView = view.findViewById(R.id.vocabulary_recyclerview)
        recyclerView.layoutManager = layoutManager
        recyclerView.setHasFixedSize(true)
        adapter = vocabularyAdapter(sourceWordid, lingalaid)



    }

    private fun dataInitialize() {

        vocabularyArraylist = arrayListOf<Vocabulary>()

        sourceWordid = arrayOf("Apple", "Banana",)


        lingalaid = arrayOf("pomme", "banane")



    }

}



vocabulary_item.XML

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="fragments.VocabularyFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vocabulary_recyclerview"
        tools:listitem="@layout/vocabulary_item"/>





</FrameLayout>

This was my mistake.这是我的错误。 I forgot to add an array of Int for sounds on the Adapter constructor.我忘记在Adapter构造函数中为声音添加一个Int数组。

class VocabularyAdapter(
    val sourceWord: Array<String>, 
    val targetWord: Array<String>, 
    val sound : Array<Int>
) : RecyclerView.Adapter<VocabularyAdapter.MyViewHolder>() {

    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvSourceWord = itemView.findViewById<TextView>(R.id.inEnglishOrFrench)
        val tvLingala = itemView.findViewById<TextView>(R.id.inLingala)
    }

    ...

}

You shouldnt create instance of MediaPlayer on adaper,it better use CallBack like interface,and get implement on your activity/fragmemt and when call onClick,call function on callback and on the acticity/fragment start create a instance of media player and... .您不应该在 adaper 上创建 MediaPlayer 的实例,它最好使用类似 CallBack 的接口,并在您的活动/片段上实现,当调用 onClick 时,在回调和活动/片段上调用 function 开始创建媒体播放器的实例并... .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM