简体   繁体   English

如何使用Kotlin在通话活动中重置回收者视图的内容

[英]How to reset the content of recycler view in calling activity using Kotlin

What I want to do: As user type something in edittext, a API call and show the response in recyclerview. 我想做什么:当用户在edittext中键入一些内容时,将调用一个API并在recyclerview中显示响应。

in OnCreate() 在OnCreate()中

 usersData= ArrayList()

 resultRecyclerView.setHasFixedSize(true)
 resultRecyclerView.layoutManager=LinearLayoutManager(
                    this@MainActivity,LinearLayoutManager.VERTICAL,false)
 itemsAdapter=adapter(this@MainActivity,usersData)
 resultRecyclerView.adapter=itemsAdapter

in OnResume() 在OnResume()中

searchText.addTextChangedListener(object : TextWatcher {
  override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
    //....
  }
  override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
    //.......
  }
  override fun afterTextChanged(s: Editable) {

     Log.v("Text is ",s.toString())
     val url="https://api.github.com/search/users?q="+s.toString().trim()+"+sort:followers"
     fetchData(url)
  }
})

Method for fetching data 数据获取方法

private fun  fetchData(url: String) {

   val tag_string_req = "string_req"
   val strReq = StringRequest(Request.Method.GET,
                url, Response.Listener<String> {
                response ->
                   Log.d(TAG, response.toString())

                   val builder = GsonBuilder()
                   val gson = builder.create()
                   //val allItems=gson.fromJson(response,AllUsers::class.java)

                   val data=gson.fromJson(response,AllUsers::class.java)
                   usersData=data.items

                   Log.v("No of users",usersData?.size.toString())

                   resultRecyclerView.removeAllViews()
                   itemsAdapter?.notifyDataSetChanged()

        }, Response.ErrorListener { error ->
            VolleyLog.d(TAG, "Error: " + error.message)
        })

        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_req)
}

But updated data is not passing in the adapter (it still have zero data). 但是更新的数据没有传递到适配器中(它仍然有零个数据)。 I getting the expected response from the API . 我从API获得预期的响应。 notifyDataSetChanged() is not updating the list. notifyDataSetChanged()不更新列表。

I thought notifyDataSetChanged also update the data automatically but no. 我以为notifyDataSetChanged也会自动更新数据,但是没有。

I need to call 我需要打电话

    itemsAdapter?.updateData(usersData)

and in adapter: 并在适配器中:

fun updateData(users: ArrayList<Items>?){
        this.users=users
    }

Add this method in adapter 在适配器中添加此方法

fun addData(exList: MutableList<Object>) {
    items.clear()
    items.addAll(exList)
    notifyDataSetChanged()
}

and add data from your activity/fragment like this 并像这样从您的活动/片段中添加数据

 projectsAdapter.addData(list as MutableList<Object>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 androidx.recyclerview.widget.RecyclerView 从 kotlin 的回收器视图中获取新活动? - How get a new activity from recycler view in kotlin using androidx.recyclerview.widget.RecyclerView? 如何使用带有 Room 的 kotlin 在回收站视图中加载 Sqlite 数据? - How to Load Sqlite data in recycler view using kotlin with Room? 在不重新加载活动的情况下更新回收站视图中的内容 - Update content in the Recycler View without reloading activity Recycler VIew 和 Adapter 打开内容不同的Activity - Recycler VIew and Adapter open Activity with different content 使用kotlin进行开发时,Android应用程序主要活动中的“回收者视图”中未显示“数据列表”。 - Data List isn't showing up in the Recycler View in the main activity in android app , using kotlin for development. 使用Kotlin在Recycler View中显示JSON值 - Display JSON value in Recycler View using Kotlin 使用 firebase - kotlin 未显示回收站视图 - Recycler View is not Showing by using firebase - kotlin 如何在回收站视图(kotlin)的末尾添加一个按钮? - How to add a button at the end of a recycler view (kotlin)? 使用 sqlite kotlin 的回收器视图 - Recycler view with sqlite kotlin Kotlin:Recycler View Choppy - Kotlin: Recycler View Choppy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM