程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

如何在片段android kotlin中只调用一次函数

发布于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黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

17 0
收藏该文
已收藏

评论内容:(最多支持255个字符)