发布于2023-01-16 07:05 阅读(1166) 评论(0) 点赞(17) 收藏(2)
当我的应用程序启动时,我调用一个使用 retrofit/gson 的函数并调用一个 rest API,然后从那里将数据发送到适配器并放置在回收器视图中。我遇到的问题是,当我导航到另一个片段并再次返回时,再次调用该函数,但旧数据保留在回收器视图中,导致当我只在回收器视图中放置相同数据的多个副本希望它调用一次或至少在回收站视图中只有一组数据
我已经尝试从活动而不是片段调用函数,将函数放入 onCreate 方法和数学 if-else 表达式都不会产生预期的效果。
我的代码:
MainFragment.kt
class MainFragment : Fragment() {
private var text1list = mutableListOf<String>()
private var text2list = mutableListOf<String>()
private var text3list = mutableListOf<String>()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_main, container, false)
view.see_codes.setOnClickListener {
findNavController().navigate(R.id.action_mainFragment_to_listFragment2)
}
TestGet()
return view
}
val BASE_URL = "https://run.mocky.io/v3/"
private fun TestGet() {
val api = Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(SimpleApi::class.java)
GlobalScope.launch(Dispatchers.IO) {
val response = api.phonehome()
try {
for (Mlist in response.Mlist){
Log.d("MainActivity", "Result + $Mlist")
addToList(Mlist.text1, Mlist.text2, Mlist.text3)
}
withContext(Dispatchers.Main){
setUpRecyclerView()
}
}catch (e: Exception){
println("you messed up the connection some how")
}
}
}
private fun setUpRecyclerView() {
main_recyclerview.layoutManager = LinearLayoutManager(this.context)
main_recyclerview.adapter = MainAdapter(text1list,text2list,text3list)
}
private fun addToList(text1: String, text2: String, text3: String){
text1list.add(text1)
text2list.add(text2)
text3list.add(text3)
}
}
我的适配器
MainAdapter.kt
class MainAdapter(
private var text1: List<String>, private var text2: List<String>, private var text3: List<String>) : RecyclerView.Adapter<MainAdapter.ViewHolder>() {
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
var itemtext1 : TextView = itemView.findViewById(R.id.note_text_view_1)
var itemtext2 : TextView = itemView.findViewById(R.id.note_text_view_2)
var itmetext3 : TextView = itemView.findViewById(R.id.note_text_View_3)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.carditem,parent,false)
return ViewHolder(v)
}
override fun getItemCount(): Int {
return text1.size
}
override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
holder.itemtext1.text = text1[position]
holder.itemtext2.text = text2[position]
holder.itmetext3.text = text3[position]
}
}
感谢您的时间。
I have believe I figured out what Is wrong with my app, but first a big thanks to Mehran B, it was he that laid down the groundwork for this solution.
The problem were the mutableListOf
's variables at the top of the code. they were holding on the data from the previous call of the function and just adding to them. So first I tried to solve the problem by clearing the data from all the mutableListOf
's. well, I couldn't find out how exactly to do that but if anyone knows how please comment.
so what I eventually did was this
if (!Settings.recViewAlreadyUpdated) {
TestGet()
Settings.recViewAlreadyUpdated = true
}else{
text1list = mutableListOf()
text2list = mutableListOf()
text3list = mutableListOf()
TestGet()
}
what I did was overite or create a new mutableListOf
(I'm not exactly sure which) if the function had not been used before. big thanks once again to Mehran B https://stackoverflow.com/users/10695663/mehran-b
作者:黑洞官方问答小能手
链接:http://www.javaheidong.com/blog/article/626581/7ac6a07089307dba769e/
来源:java黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 java黑洞网 All Rights Reserved 版权所有,并保留所有权利。京ICP备18063182号-2
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!