Linked Questions

Popular Questions

I'm creating a simple audio player. I have two RecyclerView and I want to see the list of items from my DataService, but I'm getting exception. Can not understand what went wrong.

When I click on item "Audio files 1" in previous Activity (RecyclerView) it should show me the list of "unitOneSound".

But I get an error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.nikolai.leader/com.example.nikolai.leader.Controller.SoundActivity}:

java.lang.IllegalStateException: songType must not be null

I think the problem with DataService because when I change DataService.getSong(songType) to just DataService.unitOneSound it works and shows the list of the song but for all albums the same list of song.

Adapter:

    class RecyclerSoundScreenAdapter(val context: Context, val song: List<SoundScreen>):  RecyclerView.Adapter<RecyclerSoundScreenAdapter.Holder>() {

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bindActivity(song[position], context)
    }

    override fun getItemCount(): Int {
        return song.count()
    }

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

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val songName = itemView.findViewById<TextView>(R.id.sound_text_view)

        fun bindActivity(sound: SoundScreen, context: Context) {
            songName.text = sound.songTitle
        }
    }
}

Controller

    class SoundActivity : AppCompatActivity() {

    lateinit var adapter: RecyclerSoundScreenAdapter

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

        val songType = intent.getStringExtra("activity")
        adapter = RecyclerSoundScreenAdapter(this, DataService.getSong(songType))

        val layoutManager = LinearLayoutManager(this)
        soundListView.layoutManager = layoutManager
        soundListView.adapter = adapter
    }
}

Model:

    class SoundScreen(var songTitle: String) {
    override fun toString(): String {
        return songTitle
    }
}

DataService:

 object DataService {
   val unitOneSound = listOf(
        SoundScreen("Song 1"),
        SoundScreen("Song 2"),
        SoundScreen("Song 3"),
        SoundScreen("Song 4"),
        SoundScreen("Song 5"),
        SoundScreen("Song 6")
    )

    val unitTwoSound = listOf(
        SoundScreen("Song 1"),
        SoundScreen("Song 2"),
        SoundScreen("Song 3"),
        SoundScreen("Song 4"),
        SoundScreen("Song 5"),
        SoundScreen("Song 6")
    )

   val listOfSounds = listOf<SoundScreen>()

    fun getSong(activity: String) : List<SoundScreen>{
        return when(activity){
            "Audio files 1" -> unitOneSound
            "Audio files 2" -> unitTwoSound
            else -> listOfSounds
        }
    }

First Activity, where I start my Sound activity

class SecondActivity : AppCompatActivity() {

    lateinit var adapter : RecyclerSecondScreenAdapter

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

        val activityType = intent.getStringExtra("unit")
        adapter = RecyclerSecondScreenAdapter(this, DataService.getActivities(activityType)){ activity ->
           val soundIntent = Intent(this, SoundActivity::class.java)
//            soundIntent.putExtra(EXTRA_ACTIVITY, activity)
            startActivity(soundIntent)
        }

        val layoutManager = LinearLayoutManager(this)
        activityListView.layoutManager = layoutManager
        activityListView.adapter = adapter
    }
}

Related Questions