【发布时间】:2020-08-03 03:43:42
【问题描述】:
我正在使用 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"
}
]
}
当成功获取数据时,仅当用户搜索国家并添加到列表并显示其相关数据(如乡村和人口)时才会显示。我能够获取 JSON 数据,但无法像用户搜索并添加到列表时那样控制它。这是我的主要活动
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)
}
}
})
}
}
这是我的 MainAdapter
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 编写的代码吗
【问题讨论】:
-
你的问题不是很清楚,你能详细说明你的最后一个问题吗?
-
我上面提到的代码只是在回收者视图中获取所有 json 数据。但是,我希望,在用户搜索和添加项目之前,应用程序和初始列表中的实施搜索视图将为空......
-
简单地说,我想显示某些数据而不是视图中的所有数据。并且只有当用户搜索除国家名称以外的任何内容时,数据才会按唯一的国家名称搜索,其显示错误。当用户使用国家名称搜索时,它还会显示与该国家名称相关的所有数据。比如,国家代码和人口..
-
那么,您不想在用户搜索后显示整个国家/地区列表吗?您应该通过用户查询过滤国家/地区列表,并使用过滤后的数据更新适配器。
-
@JuanMartinez 确切地说,我无法解释问题。但是,这是我想要的。而且,初始列表将为空
标签: android json android-studio kotlin search