I’m new to android development, I decided to write a music player, but it gives an error in the lines recycler_view.LayoutManager = LayoutManager and in recycler_view.adapter = songListAdapter. I don’t understand what the problem might be, could those who know tell me? Here is the code from MainActivity:
package com.example.zevamusic
import android.annotation.SuppressLint
import android.database.Cursor
import android.os.Bundle
import android.provider.MediaStore
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.example.zevamusic.adapters.SongListAdapter
import com.example.zevamusic.model.SongModel
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var songModelData:ArrayList<SongModel> = ArrayList()
var songListAdapter:SongListAdapter?=null
@SuppressLint("Range")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
var songCursor: Cursor? = contentResolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
null,null,null,null)
while (songCursor!=null && songCursor.moveToNext()){
var songName = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.TITLE))
var songDuration = songCursor.getString(songCursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
songModelData.add(SongModel(songName,songDuration))
}
songListAdapter = SongListAdapter(songModelData)
var layoutManager = LinearLayoutManager(applicationContext)
recycler_view.LayoutManager = layoutManager
recycler_view.adapter = songListAdapter
}
}
Here is the code from SongListAdapter:
package com.example.zevamusic.adapters
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import com.example.zevamusic.R
import com.example.zevamusic.model.SongModel
class SongListAdapter(SongModel:ArrayList<SongModel>):RecyclerView.Adapter<SongListAdapter.SongListViewHolder>() {
var mSongModel = SongModel
override fun getItemCount(): Int {
return mSongModel.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SongListViewHolder {
var view = LayoutInflater.from(parent!!.context).inflate(R.layout.music_row,parent,false)
return SongListViewHolder(view)
}
override fun onBindViewHolder(holder: SongListViewHolder, position: Int) {
var model = mSongModel[position]
var songName = model.mSongName
var songDuration = model.mSongDuration
holder!!.songTV.text = songName
holder.durationTV.text = songDuration
}
class SongListViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
var songTV:TextView
var durationTV:TextView
var albumnAtr:ImageView
init {
songTV = itemView.findViewById(R.id.song_name_tv)
durationTV = itemView.findViewById(R.id.song_duration_tv)
albumnAtr = itemView.findViewById(R.id.al_image_view)
}
}
}
Here is the code from SongModel:
package com.example.zevamusic.model
class SongModel(songName: String,songDuration:String) {
var mSongName = songName
var mSongDuration = songDuration
}
I am making this application based on a video clip, I noticed in it the import of which I did not have(import kotlinx.android.synthetic.main.activity_main.*), I decided to add it, but for some reason the word android is lit in red in this import and I tried to fix the problem in import, but I still didn’t figure out what the problem was.
ADES GAMES is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.