类型不匹配:推断的类型为fragment_Dep,但上下文!预期

时间:2018-11-06 13:57:42

标签: kotlin

我的应用程序中有创建带有标签的滑动视图。我想在片段类活动中初始化列表视图。我添加了列表适配器,但是问题出在以下代码上:

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

        }




    }



}

1 个答案:

答案 0 :(得分:0)

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

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

如果要通过活动创建适配器,则只需执行ListAdapter(this, <your_list>)

如果您是从片段创建的,则可以ListAdapter(this.activity, <your_list>)

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