为什么在Android设备上不显示WeekDay对象的列表? Kotlin ListView

问题描述 投票:0回答:1

我正在编写第一个Android规划应用程序。我有一个导航抽屉和一个WeekListFragment。在片段中,我创建了WeekDay对象的数组,并尝试通过WeekAdapter显示它们。但是,当我运行Android仿真器时,虽然片段打开,却什么也没显示。您能解释一下我该怎么做吗?

这里是此片段的代码:

package itshpit.com.myapplication

import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.ImageView
import android.widget.TextView
import androidx.fragment.app.Fragment
import itshpit.com.myapplication.classes.WeekDay

class WeekListFragment() : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View? = inflater.inflate(R.layout.fragment_week, container, false)
        (activity as MainActivity).supportActionBar!!.title = "Week"

        val week: Array<String> = resources.getStringArray(R.array.week)
        val weekList: ArrayList<WeekDay> = ArrayList()
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 1))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 2))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 3))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 4))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 5))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 6))
        weekList.add(WeekDay(text = "some Text", image = R.drawable.mon, id = 7))
        val adapter = WeekAdapter((activity as MainActivity), weekList)

        return view
    }

    class WeekAdapter(context: Context, val weekList: List<WeekDay>) : ArrayAdapter<WeekDay>(context, 0, weekList) {

        private class ViewHolder(row: View?) {
            var txtName: TextView
            var ivImage: ImageView

            init {
                this.txtName = row?.findViewById(R.id.txtWeekDay) as TextView
                this.ivImage = row.findViewById(R.id.imgWeekDay) as ImageView
            }
        }
        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

            val view: View?
            val viewHolder: ViewHolder
            if (convertView == null) {
                val layout = LayoutInflater.from(context)
                view = layout.inflate(R.layout.week_item, parent, false)
                viewHolder = ViewHolder(view)
                view.tag = viewHolder
            } else {
                view = convertView
                viewHolder = view.tag as ViewHolder
            }
            val weekDay: WeekDay = getItem(position) as WeekDay
            viewHolder.txtName.text = weekDay.text
            viewHolder.ivImage.setImageResource(weekDay.image)


            return view as View
        }

        override fun getItemId(position: Int): Long {
            return weekList[position].id
        }

        override fun getItem(position: Int): WeekDay? {
            return weekList.get(position)
        }

        override fun getCount(): Int {
            return 7
        }
    }
}

WeekDay的班级代码如下:

data class WeekDay(var id: Long = -1,
                   var date: Date? = null,
                   var text: String,
                   var image: Int
                   ) {}

“ fragment_week.xml”文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".WeekListFragment">

    <ListView
        android:id="@+id/weekList"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </ListView>

</RelativeLayout>
android android-studio listview kotlin adapter
1个回答
0
投票

您从未将适配器分配给ListView。将val view: View?更改为val view: ListView,然后将val adapter =更改为view.adapter =

© www.soinside.com 2019 - 2024. All rights reserved.