媒体播放器,点击片段上的监听器



我试图配置一个应用程序在一个片段的recyclerview中播放对应于单词的声音,但是我在创建它的onclick部分时遇到了麻烦。我在适配器上得到以下错误消息:";下列函数都不能用所提供的参数调用。创建(上下文!)在android.media.MediaPlayer中定义创建(上下文!, Int)定义于android.media.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>

这是我的错误。我忘了在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)
}
...
}

你不应该在adaper上创建MediaPlayer的实例,它最好使用类似CallBack的接口,并在你的activity/fragment上实现,当调用onClick时,调用CallBack函数,并在activity/fragment上开始创建media player的实例... .

相关内容

  • 没有找到相关文章

最新更新