kotlin - 在片段 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
}
}
解决方案
根据文档,有单个参数的方法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
永远不应该为空。
此视图最终将附加到的父级
推荐阅读
- r - 如何为栅格设置正确的投影?
- excel - 从多个工作表中查找同一列的总计,并在新工作表的工作表名称旁边表示总计
- c# - 我的 Azure API(文本翻译、计算机视觉)的端点不可访问(错误代码 404)
- java - QuerySyntaxException:意外令牌:使用标准构建器在 postgresql 中的日期之间产生差异
- c# - 使用需要传递数组的 webservice(wsdl)
- java - 如何修复“错误:
- 内部元素必须是资源引用或为空。”?
- reactjs - 如何使用 React 解决“未提供令牌”消息?
- perl - Perl 内部变量。下一个表达式是否相同?
- php - laravel 上的本地化 URL 问题
- r - 在 R 中使用 anova() 进行模型比较时出现错误消息