Android layout 我只需要打印kotlin android中的if条件
我是android kotlin的新手,我有一个活动,只显示表中有(国家代码=1)的行,我在if条件中也提到过,但它会打印表中的所有值 这是我的活动代码Android layout 我只需要打印kotlin android中的if条件,android-layout,android-studio,kotlin,android-recyclerview,Android Layout,Android Studio,Kotlin,Android Recyclerview,我是android kotlin的新手,我有一个活动,只显示表中有(国家代码=1)的行,我在if条件中也提到过,但它会打印表中的所有值 这是我的活动代码 package com.tripbegins.tripapp import android.support.v7.widget.RecyclerView import android.view.LayoutInflater import android.view.View import android.view.ViewGroup
package com.tripbegins.tripapp
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.visa_details_description.view.*
class VisaView(val visafeed: Homefeed):
RecyclerView.Adapter<CustomVisaViewHolder>() {
override fun getItemCount(): Int {
return visafeed.visa.count()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CustomVisaViewHolder {
val layoutInflater = LayoutInflater.from(parent?.context)
val valueRow = layoutInflater.inflate(R.layout.visa_details_description, parent, false)
return CustomVisaViewHolder(valueRow)
}
override fun onBindViewHolder(holder: CustomVisaViewHolder?, position: Int) {
val visaDescription = visafeed.visa.get(position)
if (visaDescription.country_id == 1) {
holder?.visaDescriptionView?.description_view?.text = visaDescription.description
}
}
}
class CustomVisaViewHolder(val visaDescriptionView: View?) :
RecyclerView.ViewHolder(visaDescriptionView) {
}
我的预期结果
输出应仅显示等于国家代码=1的值
15天单次入境
30天多次入境
当前结果
它将显示country_code=1值以及所有行
15天单次入境
30天多次入境
30天单次入境
30天单次入境
30天单次入境
30天单次入境
30天单次入境
30天单次入境如果您的目标是只显示countryId==1的行,那么您应该在您的
VisaView
(这是一个适配器
,而不是视图
)中只添加符合该条件的签证。您可能要做的是添加所有行(但您没有提供创建VisaView
的代码片段,因此我的只是一个猜测)
编辑
根据您的代码,可能的解决方案之一大致如下:
1) 更改VisaView
(也应重命名为VisaAdapter
或类似名称,因为它不是视图
,而是适配器
),以保存VisaDetails
列表:
class VisaView(val visa: List<VisaDetails>) :
RecyclerView.Adapter<CustomVisaViewHolder>() {
override fun getItemCount(): Int {
return visa.count()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CustomVisaViewHolder {
val layoutInflater = LayoutInflater.from(parent?.context)
val valueRow = layoutInflater.inflate(R.layout.visa_details_description, parent, false)
return CustomVisaViewHolder(valueRow)
}
override fun onBindViewHolder(holder: CustomVisaViewHolder?, position: Int) {
val visaDescription = visa[position]
if (visaDescription.country_id == 1) {
holder?.visaDescriptionView?.description_view?.text = visaDescription.description
}
}
}
class CustomVisaViewHolder(val visaDescriptionView: View?) :
RecyclerView.ViewHolder(visaDescriptionView) {
}
谢谢你的快速回复。您能否帮助我在适配器中添加条件的代码。您能否提供创建
VisaView
实例的代码?一种方法应该是newvisaview(visafeed.visa.filter{it.country\u id==1})
,您还应该将VisaView
实现更改为保存visa的收集,而不是Homefeed
。然而,这不是唯一可能的解决办法,我还有另一个表格国家:[{id:“1”,国家:“马来西亚”},{id:“2”,国家:“迪拜”},{id:“3”,国家:“亚美尼亚”},{id:“4”,国家:“格鲁吉亚”},如果我想检查国家id(表格签证的)=id(表格国家的)。你能提供一个解决方案吗@用户2468312读取该表时,需要将国家id保存到变量中。然后,您将使用该变量来过滤我提供的第二个代码段中的VISA列表,而不是使用硬编码值1
。否则,您可以使用SQL执行所有操作,只检索您感兴趣的VISA描述,因为我是Android新手。请您帮助检查两个表之间或sql中的值的代码。请做必要的事@用户2468312
visa: [
{
id: "1",
country_id: "1",
description: "15 DAYS SINGLE ENTRY"
},
{
id: "2",
country_id: "1",
description: "30 DAYS MULTI ENTRY"
},
{
id: "4",
country_id: "2",
description: "30 DAYS SINGLE ENTRY"
},
{
id: "5",
country_id: "2",
description: "90 DAYS SINGLE ENTRY"
},
{
id: "6",
country_id: "3",
description: "21 Days Single Entry"
},
{
id: "7",
country_id: "3",
description: "120 Days Single Entry"
},
{
id: "8",
country_id: "2",
description: "30 Days Express Visa"
},
{
id: "9",
country_id: "2",
description: "90 Days Express Visa"
}
],
class VisaView(val visa: List<VisaDetails>) :
RecyclerView.Adapter<CustomVisaViewHolder>() {
override fun getItemCount(): Int {
return visa.count()
}
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): CustomVisaViewHolder {
val layoutInflater = LayoutInflater.from(parent?.context)
val valueRow = layoutInflater.inflate(R.layout.visa_details_description, parent, false)
return CustomVisaViewHolder(valueRow)
}
override fun onBindViewHolder(holder: CustomVisaViewHolder?, position: Int) {
val visaDescription = visa[position]
if (visaDescription.country_id == 1) {
holder?.visaDescriptionView?.description_view?.text = visaDescription.description
}
}
}
class CustomVisaViewHolder(val visaDescriptionView: View?) :
RecyclerView.ViewHolder(visaDescriptionView) {
}
recylerView.adapter = VisaView(visafeed.visa.filter { it.country_id == 1 }) // the "1" can and should be parameterized in case you need to reuse the code