【问题标题】:Type mismatch: inferred type is fragment_Dep but Context! was expected类型不匹配:推断类型是 fragment_Dep 但 Context!预计
【发布时间】:2018-11-06 13:57:42
【问题描述】:

我在我的应用中创建了带有标签的滑动视图。我想在片段类活动中初始化列表视图。我添加了列表适配,但问题在于这段代码:

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

我有问题 context 我有错误 Type mismatch: inferred type is fragment_Dep but Context! was expected

ListAdapte 类

package com.iraqairoirt.iraqairports


import android.annotation.SuppressLint
import android.support.v7.widget.AppCompatTextView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter

class ListAdapte (val context: fragment_Dep, val list: ArrayList<FlightShdu>): BaseAdapter() {

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

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

        val CallsingID = view.findViewById(R.id.callsign_id) as AppCompatTextView
        val StatusID = view.findViewById(R.id.status_id) as AppCompatTextView

        CallsingID.text = list[position].Callsign.toString()
        StatusID.text = list[position].Status

        return view
    }

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

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

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

片段类

package com.iraqairoirt.iraqairports

import android.os.AsyncTask
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

import com.iraqairoirt.iraqairports.flightsArr.ListAdapteArr
import kotlinx.android.synthetic.main.fragment_arrivel.*
import org.json.JSONArray
import org.json.JSONObject
import java.net.HttpURLConnection
import java.net.URL


@Suppress("UNREACHABLE_CODE")
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 = "xxxxxxxxxxxxxxx/airport.json?code=BGW"
        Dep().execute(url)

        return view
    }


    //    full class for json api
    inner class Dep : AsyncTask<String, String, String>(){


        override fun onPreExecute() {
            super.onPreExecute()


        }

        //        for build connection

        override fun onPostExecute(result: String?) {

            super.onPostExecute(result)
            handleJson(result)


        }

        override fun onProgressUpdate(vararg text: String?) {


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


        private fun handleJson (jsonString: String?){

            val jsonObj = JSONObject(jsonString)
            val result = jsonObj.getJSONObject("result")
            val response = result.getJSONObject("response")
            val airport = response.getJSONObject("airport")
            val pluginData = airport.getJSONObject("pluginData")
            val schedule = pluginData.getJSONObject("schedule")
            val departures = schedule.getJSONObject("departures")
//        val data = arrivals.getJSONObject("data")
            val jsonArray = JSONArray(departures.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").getString("text"),
                    jsonObject.getJSONObject("flight").getJSONObject("airline").getJSONObject("code").getString("icao"),
                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("scheduled").getString("departure"),
                    jsonObject.getJSONObject("flight").getJSONObject("airport").getJSONObject("destination").getJSONObject("code").getString("iata"),
                    jsonObject.getJSONObject("flight").getJSONObject("aircraft").getJSONObject("model").getString("code"),
//                    for more information
                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("real").getString("departure"),
                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("departure"),
//                    jsonObject.getJSONObject("flight").getJSONObject("time").getJSONObject("estimated").getString("arrival"),
                    jsonObject.getJSONObject("flight").getJSONObject("aircraft").getString("registration"),
                    jsonObject.getJSONObject("flight").getJSONObject("status").getJSONObject("generic").getJSONObject("status").getString("diverted"),
                    departures.getString("timestamp"),
                    jsonObject.getJSONObject("flight").getJSONObject("status").getString("icon")



                ))


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

            val adapter = ListAdapteArr(list)
            flight_arrivel_list.adapter = adapter

        }




    }



}

【问题讨论】:

  • 您的问题是什么?错误很明显
  • 如何修复错误?我是 kotlin 的新手?
  • 传递上下文而不是fragment_Dep
  • @TimCastelijns 有什么例子吗?

标签: kotlin


【解决方案1】:

您可以简单地将Context 传递到 ListAdapter 签名中

class ListAdapter(val context: Context, val list: ArrayList<FlightShdu>): BaseAdapter() {

如果你是从一个活动创建适配器,你可以简单地做ListAdapter(this, &lt;your_list&gt;)

如果你是从片段创建它,你可以这样做ListAdapter(this.activity, &lt;your_list&gt;)

我建议你熟悉 Kotlin 语言https://kotlinlang.org/docs/reference/

【讨论】:

  • 感谢您的回答,我对class ListAdapter(val context: Context, val list: ArrayList&lt;FlightShdu&gt;): BaseAdapter() 执行此代码,并且类适配器中的错误为隐藏。但是我在类片段中有错误。单词表下的红线val adapter = ListAdapteArr(list) flight_arrivel_list.adapter = adapter
  • 你的listAdapteArr在哪里?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-16
  • 1970-01-01
相关资源
最近更新 更多