如何解决对两个不同的RecyclerView和两个不同的Retrofit2响应主体使用一个函数
//first use
fun showData(teams: List<proc1>) {
recyclerView.apply {
layoutManager = linearlayoutmanager(context)
adapter = TeamsAdapter(teams)
}
}
// second use
fun showData(compteams: List<compteams>) {
recyclerView.apply {
layoutManager = linearlayoutmanager(context)
adapter = CompTeamsAdapter(compteams)
}
}
如何将其转换为针对两个(或更多)不同列表的单个共享功能。如您所见,这两个列表是List:proc1和List:compteams,两个不同的recyclerViews具有两个不同的适配器,TeamsAdapter和CompTeamsAdapter,分别显示在下面:
class TeamsAdapter(private val teams: List<proc1>) :
RecyclerView.Adapter<TeamsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): TeamsAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.team_row,parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: TeamsAdapter.ViewHolder,position: Int) {
val team = teams[position]
// println("hello ${team::class}")
val context = holder.imageView.context
val filename = team.field2!!.toLowerCase(Locale.ROOT)
val drawid = context.getResources().getIdentifier(
filename,"drawable",context.getPackageName()
)
holder.imageView.setimageResource(drawid)
holder.field1.text = team.field1
holder.field2.text = team.field2
}
override fun getItemCount() = teams.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView: ImageView = itemView.image_view
val field1: TextView = itemView.field1
val field2: TextView = itemView.field2
}
}
class CompTeamsAdapter(private val compteams: List<compteams>) :
RecyclerView.Adapter<CompTeamsAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): CompTeamsAdapter.ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.comp_teams_row,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: CompTeamsAdapter.ViewHolder,position: Int) {
val compteam = compteams[position]
// println("hello ${compteam::class}")
val context = holder.imageView1.context
val filename1 = compteam.team1!!.toLowerCase(Locale.ROOT)
val filename2 = compteam.team2!!.toLowerCase(Locale.ROOT)
val drawid1 = context.getResources().getIdentifier(
filename1,context.getPackageName())
val drawid2 = context.getResources().getIdentifier(
filename2,context.getPackageName())
holder.imageView1.setimageResource(drawid1)
holder.imageView2.setimageResource(drawid2)
holder.team1.text = compteam.team1
holder.team2.text = compteam.team2
holder.score1.text = compteam.pf
holder.score2.text = compteam.pa
holder.date11.text = compteam.date11
holder.record.text = compteam.record
holder.game.text = compteam.game
}
override fun getItemCount() = compteams.size
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val imageView1: ImageView = itemView.image_team1
val imageView2: ImageView = itemView.image_team2
val team1: TextView = itemView.data_team1
val team2: TextView = itemView.data_team2
val score1: TextView=itemView.data_score1
val score2: TextView=itemView.data_score2
val date11: TextView=itemView.data_date11
val record: TextView=itemView.data_record
val game:TextView=itemView.data_game
}
}
**I hate that I have such similar logic in two different places. Ideally,I would have one adapter,and one showData routine. and they would be designed to handle any recyclerView and any List. Below I will show where showData is called from two different functions:**
object CommonSubs {
fun getTeams(context: Context,recyclerView: RecyclerView) {
fun showData(teams: List<proc1>) {
recyclerView.apply {
layoutManager = linearlayoutmanager(context)
adapter = TeamsAdapter(teams)
}
}
val TAG = "NFL - List Teams"
val newKVT = kvtproc1()
newKVT.NGprojID = "Demo"
newKVT.NGprocID = "Proc1"
newKVT.demovar = "Thomas"
val newBIS = bisproc1(spKVs = newKVT)
val api = RetrofitBuilder.buildService(ApiService::class.java)
val requestCall: Call<List<proc1>> = api.postBISteams(newBIS)
requestCall.enqueue(object : Callback<List<proc1>> {
// If you receive an HTTP response,then this method is executed
// Your HTTP STATUS Code will decide if your HTTP response is a Success or an Error
override fun onResponse(call: Call<List<proc1>>,response: Response<List<proc1>>) {
Log.d(TAG,"OkHttp response from BIS")
when {
response.isSuccessful -> {
Log.d(TAG,"OkHttp response from BIS isSuccessful")
// the HTTP STATUS Code is in the range 200 to 299
var BISresponse = response.body() // Use it or ignore it
Log.d("NFL BIS pretty json =>"," \n\r" + BISresponse)
Log.d(
"NFL BIS pretty json =>"," \n\r" +
GsonBuilder().setPrettyPrinting().create().toJson(BISresponse)
)
Toast.makeText(context,"Request Complete",Toast.LENGTH_LONG).show()
showData(response.body()!!)
}
response.code() == 401 -> { //appplication level failure
// the HTTP STATUS Code is in the range of 300's,400's,or 500's
Toast.makeText(
context,"OkHttp session has expired.",Toast.LENGTH_LONG
).show()
}
else -> { //application level failure
// the HTTP STATUS Code is in the range of 300's,or 500's
Toast.makeText(context,"Unexpected OkHttp Response.",Toast.LENGTH_LONG)
.show()
}
}
}
override fun onFailure(call: Call<List<proc1>>,t: Throwable) {
Log.d(TAG,"OkHttp response from BIS Failed$t")
Toast.makeText(
context,"Failed to connect - System failure.",Toast.LENGTH_LONG
).show()
}
})
}
fun getCompTeams(context: Context,recyclerView: RecyclerView,team1:String,team2:String) {
fun showData(compteams: List<compteams>) {
recyclerView.apply {
layoutManager = linearlayoutmanager(context)
adapter = CompTeamsAdapter(compteams)
}
}
val TAG = "NFL - List Teams"
val newKVT = kvtcompteams()
newKVT.NGprojID = "NFL"
newKVT.NGprocID = "COMPTEAMS"
newKVT.team1 = team1
newKVT.team2 = team2
val newBIS = biscompteams(spKVs = newKVT)
val api = RetrofitBuilder.buildService(ApiService::class.java)
val requestCall: Call<List<compteams>> = api.postBIScompteams(newBIS)
requestCall.enqueue(object : Callback<List<compteams>> {
// If you receive an HTTP response,then this method is executed
// Your HTTP STATUS Code will decide if your HTTP response is a Success or an Error
override fun onResponse(
call: Call<List<compteams>>,response: Response<List<compteams>>
) {
Log.d(TAG,Toast.LENGTH_LONG)
.show()
}
}
}
override fun onFailure(call: Call<List<compteams>>,Toast.LENGTH_LONG
).show()
}
})
}
}
最后,我想展示两种不同的retrofit2 API服务,这些服务理想情况下可以组合成具有适当参数的单个函数:
interface ApiService {
@POST("NYCWAYJSON")
fun postBISteams(@Body newBIS: bisproc1): Call<List<proc1>>
@POST("NYCWAYJSON")
fun postBIScompteams(@Body newBIS: biscompteams): Call<List<compteams>>
}
这都是工作逻辑,但是随着我在后端添加越来越多的API,当这些都是通用的例程时,我不想继续添加越来越多的代码。当我在后端添加新的API时,我应该能够添加新的匹配数据类,新的recyclerView行布局,并且仅此而已。我不应该每次都重复这些大例程。我还没有KOTLIN或ANDROID技能来组合这些内容。请帮忙! ts
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。