首页 > 解决方案 > 在片段 kotlin 中显示列表视图

问题描述

我想在片段中显示我的列表视图,我使用了分离的列表视图 adpater。

我在行下的类适配器中遇到错误val view : View = LayoutInflater.from(context,this).inflate(R.layout.arr_list,parent,false)

类片段

class fragment_Arr :Fragment(), View.OnClickListener {
        override fun onClick(v: View?) {
    //        val intent = Intent(context, FlightsArrbefor::class.java)
    //        context!!.startActivity(intent)
        }


        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val view = inflater.inflate(R.layout.fragment_arrivel,container,false)


            val url = "xxxxxxxx/airport.json?code=BGW"
            Arr().execute(url)

            return view
        }
        inner class Arr : AsyncTask<String, String, String>(){



            override fun onPreExecute() {
                super.onPreExecute()



            }

            //        for build connection
            override fun doInBackground(vararg url: String?): String{

                var text : String
                val connection = URL(url[0]).openConnection() as HttpURLConnection

                try {
                    connection.connect()
                    text = connection.inputStream.use { it.reader().use{reader -> reader.readText()} }


                } finally{

                    connection.disconnect()

                }
                return text
            }

            override fun onPostExecute(result: String?) {

                super.onPostExecute(result)
                handleJson(result)


            }

            override fun onProgressUpdate(vararg text: String?) {


            }
            @SuppressLint("WrongViewCast")
            private fun handleJson (jsonString: String?) {

                val jsonObj = JSONObject(jsonString)
                val result = jsonObj.getJSONObject("result")
                val response = result.getJSONObject("respe")
                val airport = response.getJSONObject("airport")
                val pluginData = airport.getJSONObject("Data")
                val schedule = pluginData.getJSONObject("schedule")
                val arrivals = schedule.getJSONObject("arrivals")

    //        val data = arrivals.getJSONObject("data")
                val jsonArray = JSONArray(arrivals.get("data").toString())

                val list = ArrayList<FlightShdu>()
                var x = 0
                while (x < jsonArray.length()) {

                    val jsonObject = jsonArray.getJSONObject(x)



                    list.add(
                        FlightShdu(
                            jsonObject.getJSONObject("flight").getJSONObject("identification").getJSONObject("number").getString("default"),
                            jsonObject.getJSONObject("flight").getJSONObject("airline").getString("short"),
                            jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject("status" )

                        )
                    )


                    x++
                }
                list.forEach(::println)

                var adapter = ListAdapteArr(this@MainActivity, list)
                flight_arrivel_list.adapter = adapter




            }

        }

列出适配器类

class ListAdapteArr (val context: fragment_Arr, var list: ArrayList<FlightShdu>): BaseAdapter() {

    @SuppressLint("ViewHolder", "NewApi")
    override fun getView(p0: Int, convertView: View?, parent: ViewGroup?): View {

        val view : View = LayoutInflater.from(context,this).inflate(R.layout.arr_list,parent,false)

           val list = list[p0]

        val code = view.findViewById(R.id.code_id) as AppCompatTextView

        view.callsign_id.text=list.Callsign
        view.airline_id.text=list.Airline
        code.text = list.code


        view.setOnClickListener {

            val intent = Intent(context, FlightDeatilasArr::class.java)

            intent.putExtra("Stauts",list.Stauts!!)
            intent.putExtra("Callsign",list.Callsign!!)
            intent.putExtra("Airline",list.Airline!!)

            context!!.startActivity(intent)
        }











    }

    private fun getDateTime(s: String): String? {
        try {
            val sdf = SimpleDateFormat("EE, MMM d KK:mm a")
            val netDate = Date(s.toLong() * 1000)
            return sdf.format(netDate)
        } catch (e: Exception) {
            return e.toString()

        }
    }

    override fun getItem(p0: Int): Any {
        return list [p0]
    }

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

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



}

标签: kotlin

解决方案


根据文档,有单个参数的方法LayoutInflater.html#from(android.content.Context),但您使用 2 个参数调用它

LayoutInflater.from(context,this).inflate(R.layout.arr_list,parent,false)

顺便说一句,context您传递的实际上不是Context,而是一个片段

类 ListAdapteArr (val context: fragment_Arr, ...)

要解决您的问题,请使用下一个方法

override fun getView(p0: Int, convertView: View?, parent: ViewGroup?): View? {
   val view: View = LayoutInflater.from(parent!!.context).inflate(R.layout.arr_list,parent,false)
   ...
   return view
}

根据文档parent永远不应该为空。

此视图最终将附加到的父级


推荐阅读