RecyclerView无法读取属性以分配值

时间:2019-01-23 15:46:40

标签: android android-fragments kotlin android-recyclerview android-adapter

我正在尝试让我的RecyclerView适配器从片段中的Array获取值,但是由于某种原因它不起作用,并且出现此错误(我认为我迷路了):

  

lateinit property dataTitles has not been initialized

在这种情况下,获取数据的正确方法是什么?

片段类

class MyFragment : androidx.fragment.app.Fragment() {
    private lateinit var mRecyclerView: RecyclerView

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_rv, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        val v = view

        val dataTitles: ArrayList<String> = ArrayList()
        val dataSubtitles: ArrayList<String> = ArrayList()

        mRecyclerView = v!!.findViewById<RecyclerView>(R.id.my_recyclerview)

        // Set the linear layout manager
        mRecyclerView.layoutManager = LinearLayoutManager(activity)

        // create the adapter
        val mAdapter = MyRVAdapter()


        // init data
        dataTitles.add(getString(R.string.addition))
        dataTitles.add(getString(R.string.subtraction))
        dataTitles.add(getString(R.string.multiplication))
        dataTitles.add(getString(R.string.division))

        dataSubtitles.add(getString(R.string.addition_information))
        dataSubtitles.add(getString(R.string.subtraction_information))
        dataSubtitles.add(getString(R.string.multiplication_information))
        dataSubtitles.add(getString(R.string.division_information))

        // Set the adapter
        mRecyclerView.adapter = mAdapter

        super.onActivityCreated(savedInstanceState)
    }
}

适配器类

class MyRVAdapter(private val dataTitles: Array<String>, private val dataSubtitles: Array<String>): RecyclerView.Adapter<MyRVAdapter.MyViewHolder>() {
    private val typeItem = 1
    private val typeHeader = 2
    private val typeGrid = 3

    private val primeNumbers = arrayOf("2", "3", "5", "7", "11", "13",
            "17", "19", "23")


    private lateinit var adapterGV: MyAdapter

    override fun onCreateViewHolder(parent: ViewGroup, type: Int): MyRVAdapter.MyViewHolder {
        return when (type) {
            typeHeader -> MyRVAdapter.MyViewHolder(inflateHelper(R.layout.rv_header, parent))

            typeItem -> MyRVAdapter.MyViewHolder(inflateHelper(R.layout.rv_item, parent))

            typeGrid -> MyRVAdapter.MyViewHolder(inflateHelper(R.layout.rv_gv, parent))

            else -> MyRVAdapter.MyViewHolder(inflateHelper(R.layout.rv_item, parent))
        }
    }

    override fun onBindViewHolder(viewHolder: MyRVAdapter.MyViewHolder, position: Int) {
        val llGV = viewHolder.itemView.findViewById<LinearLayout>(R.id.ll_gv)

        val llTV = viewHolder.itemView.findViewById<LinearLayout>(R.id.ll_tv)

        val mGridView = viewHolder.itemView.findViewById<GridView>(R.id.my_gv)


        when (getItemViewType(position)) {
            typeHeader -> {
                val tValueE = TypedValue()
                // test
                llTV.context.theme.resolveAttribute(R.attr.imgUnfoldMore, tValueE, true)

                val tValueC = TypedValue()
                // test
                llTV.context.theme.resolveAttribute(R.attr.imgUnfoldLess, tValueC, true)
            }
            typeGrid -> {
                val titleG = viewHolder.itemView.findViewById<TextView>(R.id.tv_gv_A)
                titleG.setText(R.string.prime_numbers)

                //
                val cardViewGV = viewHolder.itemView.findViewById<CardView>(R.id.cv_gv)
                val mLinearLayoutGV = viewHolder.itemView.findViewById<LinearLayout>(R.id.cardview_gv_titlerow

                mGridViewA.isEnabled = false
                mGridViewA.isVerticalScrollBarEnabled = false
//                adapterGV = MyAdapter(activity!!.applicationContext, 0)
                mGridViewA.adapter = adapterGV
                for (ldnBusesRoute in ldnBusesRoutes) {
                    adapterGV.addAdapterItem(AdapterItem(ldnBusesRoute))
                }
                typeItem -> {
                    // get the current item
                    val itemA = dataTitles[position - 2]
                    val itemB = dataSubtitles[position - 2]

                    //
                    val txtTitle = viewHolder.itemView.findViewById<TextView>(R.id.tv_title)
                    txtTitle.text = itemA

                    val txtSubtitle = viewHolder.itemView.findViewById<TextView>(R.id.tv_subtitle)
                    txtSubtitle.text = itemB
            }
        }
    }

    private fun inflateHelper(resId: Int, parent: ViewGroup): View {
        return LayoutInflater.from(parent.context).inflate(resId, parent, false)
    }

    // inner class for view holder to use,
    class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)


    // Adapter for GridView
    private inner class MyAdapter internal constructor(context: Context, textviewid: Int) : ArrayAdapter<AdapterItem>(context, textviewid) {
        private val items = ArrayList<AdapterItem>()

        internal fun addAdapterItem(item: AdapterItem) {
            items.add(item)
        }

        override fun getCount(): Int {
            return items.size
        }

        override fun getItem(position: Int): AdapterItem? {
            return items[position]
        }

        override fun getItemId(position: Int): Long {
            return position.toLong()
        }

        override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
            val rowView: View = when (convertView) {

                null -> LayoutInflater.from(parent.context).inflate(R.layout.gridview_item, parent, false)
//                    activity!!.layoutInflater.inflate(R.layout.gridview_item, parent, false)
                else -> convertView
            }

            val tv = rowView.findViewById(R.id.item_gridview) as TextView
            tv.text = items[position].first

            return rowView
        }
    }

    internal inner class AdapterItem // add more items
    (var first: String)

    override fun getItemCount(): Int {
        return dataTitles.size + 2
    }

    override fun getItemViewType(position: Int): Int {
        return when (position) {
            0 -> typeHeader
            1 -> typeGrid
            else -> typeItem
        }
    }
}

1 个答案:

答案 0 :(得分:0)

好吧,错误非常简单:您没有初始化dataTitles。当您用lateinit标记变量时,Kotlin希望您稍后对其进行初始化。这是一种避免将变量声明为可为空的方法。

要初始化它,您可以执行以下操作:

private val mutableList = mutableListOf<String>()

并且您正在使用dataSubtitles做同样的事情。