DataService,两个RecyclerView,看不到第二个的项目列表

时间:2019-02-03 05:12:59

标签: android kotlin androidx

我正在创建一个简单的音频播放器。我有两个RecyclerView,我想查看DataService中的项目列表,但出现异常。无法理解出了什么问题。

当我在上一个活动(RecyclerView)中单击“音频文件1”项时,应该显示“ unitOneSound”列表。

但是我得到一个错误:

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

java.lang.IllegalStateException:songType不能为空

我认为DataService存在问题,因为当我将DataService.getSong(songType)更改为DataService.unitOneSound时,它可以工作并显示歌曲列表,但对于所有专辑,歌曲列表相同。

适配器:

    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
        }
    }
}

控制器

    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
    }
}

型号:

    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
        }
    }

第一次活动,我从这里开始我的声音活动

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
    }
}

0 个答案:

没有答案