У меня есть приложение «Создание просмотров с помощью вкладок» в моем приложении. Я хочу инициализировать представление списка в деятельности класса фрагмента. Я добавил список, но проблема в следующем коде:
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 class
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
}
}
}