Search Json使用Kotlin获取结果数据



我正在使用OKhttp从URL获取JSON。。。我正在使用Kotlin

{
"CountryName": [{
"Country": "India",
"CountryCode": "+91",
"Population": "545121546846"
},
{
"country ": "Pakistan",
"countryCode": "+92",
"Population": "23546546546"
},
{
"Country": "UK",
"CountryCode": "+42",
"Population": "545121546846"
},
{
"Country": "US",
"CountryCode": "+1",
"Population": "54512154545846"
}

]

}

当成功获取数据时,只有当用户搜索Country并添加到列表中并显示其相关数据(如农村和人口(时,才会显示该数据。我可以获取JSON数据,但无法像用户搜索和添加到List那样控制它这是我的主要活动

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
recyclerView.layoutManager = LinearLayoutManager(this)
fetchjson()
}

//这就是我获取JSON 的方式

fun fetchjson() {
val url = "https://firebasestorage.googleapis.com/v0/b/unity-b69ff.appspot.com/o/new.json?alt=media&token=dc24acb2-13aa-40da-b0c5-7bb3ccd59af8"
val request = Request.Builder().url(url).build()
val client = OkHttpClient()
client.newCall(request).enqueue(object: Callback {
override fun onFailure(call: Call, e: IOException) {
println("Fail to load json")
}
override fun onResponse(call: Call, response: Response) {
val body = response.body?.string()
println(body)
val gson = GsonBuilder().create()
val homefeed = gson.fromJson(body, HomeFeed::class.java)
runOnUiThread{
recyclerView.adapter = MainAdapter(homefeed)
}
}
})
}
}

这是我的主适配器

class adapter(val homefeed: jjson) : RecyclerView.Adapter 
<adapter.CustomViewHolder>(), Filterable {
override fun getItemCount(): Int {
return homefeed.CountryName.count()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
val cellforRow = LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)
return CustomViewHolder(cellforRow)
}
override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
val v = homefeed.CountryName.get(position)
holder.itemView.textView2.text = v.Country
holder.itemView.textView.text = v.CountryCode.toString()
}
class CustomViewHolder(view: View) : RecyclerView.ViewHolder(view) {}
// this filter method 
override fun getFilter(): Filter {
return (object: Filter(){
override fun performFiltering(constraint: CharSequence?): FilterResults {
// TODO("Not yet implemented")
}
override fun publishResults(constraint: CharSequence?, results: FilterResults?) {
// TODO("Not yet implemented")
}
})
}

}
//json data object
class jjson(val CountryName: List<name>)
class name{
val Country: String? = null
val CountryCode: Int? = null
val Popuplation: Long? = null
}

获取后,我可以执行一些用JSON编写的代码吗

现在我对你的问题有了更好的理解,我正在写一个答案。

请记住,您的适配器只负责显示一组数据,在本例中是您希望用户看到的国家列表。目前,您只是在收到服务器的响应后,用完整的国家/地区列表初始化适配器。您可以使用一个空列表初始化适配器,在从服务器获取国家/地区时更新适配器数据集,然后在每次用户搜索查询更改时再次更新。您可能需要使用不同类型的适配器(例如ArrayAdapter(,以便更容易地使用列表数据。您可以在此处阅读更多信息:https://developer.android.com/guide/topics/ui/declaring-layout#AdapterViews

设置好适配器后,您需要做的是:1.用一个空列表初始化它,它可以在它的构造函数上完成
2.当国家/地区来自服务器时更新其数据集:

adapter.clear()
adapter.addAll(countries)
adapter.notifyDataSetChanged()
  1. 当用户进行搜索时更新其数据集:
adapter.clear()
adapter.addAll(yourFilteredCountries) // you can keep the whole list of countries in a variable in the activity and just pass a filtered list here
adapter.notifyDataSetChanged()

请注意,这是一种非常简单的方法,您可以通过多种方式对其进行改进,但它将帮助您了解适配器是如何工作的。

让我知道进展如何

相关内容

最新更新