简体   繁体   English

如何通过 kotlin 中的广播接收器将对象列表从适配器传递到活动

[英]How to pass a list of objects from adapter to activity via broadcast receiver in kotlin

I have a situation where I need to pass a list from adapter to activity, then pass that same list from activity to another adapter.我有一种情况,我需要将一个列表从适配器传递到活动,然后将同一个列表从活动传递到另一个适配器。 So, I am passing a list from adapter to activity like this, and it works.所以,我将一个列表从适配器传递到这样的活动,它可以工作。

if(filteredList.isNotEmpty()){
var json = Gson().toJson(filteredList)
intentPassList.putExtra("filteredList", json)
LocalBroadcastManager.getInstance(holder.context).sendBroadcast(intentPassList)
}

Next, I am receiving my list successfully like this in my activity class接下来,我在我的活动 class 中成功收到了我的列表

  private val receiverFilteredList = object : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            var recList = intent.getStringExtra("filteredList")
            if (recList != null){
                var token: TypeToken<ArrayList<Model?>?> = object : TypeToken<ArrayList<Model?>?>() {}
                myFilteredList = Gson().fromJson(recList, token.type) // list is received correctly
            }
        }
    }

Now I need to pass this received list to another adapter (recycler view) and my idea was to do this in onCreate in my activity现在我需要将此接收到的列表传递给另一个适配器(回收器视图),我的想法是在我的活动中的 onCreate 中执行此操作

private var myFilteredList: ArrayList<Model> = arrayListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
LocalBroadcastManager.getInstance(this).registerReceiver(receiverFilteredList, IntentFilter("filteredList"))

 if (myFilteredList?.isNotEmpty() == true){
            adapter = NewAdapter(myFilteredList!!)
            myRecycler?.layoutManager = manager
            myRecycler?.adapter = adapter
        } else {
            adapterInfo = NewAdapter(completeList)
            myRecycler?.layoutManager = manager
            myRecycler?.adapter = adapter
        }
}
}

but it always sends completeList to adapter, because myFilteredList is null.但它总是将 completeList 发送到适配器,因为 myFilteredList 是 null。 How can i correctly pass received list in receiver to myFilteredList in onCreate?如何正确地将接收器中的接收列表传递给 onCreate 中的 myFilteredList?

NewAdapterClass新适配器类

class NewAdapter(var itemList: ArrayList<Model>) :
    RecyclerView.Adapter<NewAdapter.ViewHolder>() {

    private lateinit var getContext: Context

    class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
        var x: TextView = view.txtX
        val y: TextView = view.txtY
        val z: TextView = view.txtZ
        val localPreferances = PreferencesManager()
        val context: Context = view.context
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        getContext = parent.context
        val layoutView = LayoutInflater.from(parent.context).inflate(R.layout.listview_item_list_layout, parent, false)
        return ViewHolder(layoutView)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {

            var receivedList = itemList[position]

            if (receivedList?.dataA != null) {
                holder.y?.text = "${receivedList?.dataA}"
            } else holder.y?.text = ""

            if (receivedList?.dataB != null) {
                holder.x?.text = "${receivedList?.dataB}"
            } else holder.x?.text = ""

            if (receivedList?.dataC != null) {
                holder.z?.text = "${receivedList?.dataC}"
            } else holder.z?.text = ""
    }

        override fun getItemCount() = itemList.size
    }

You need to inform Adapter about changes to it's data source which is List in your case, also myFilteredList = Gson().fromJson(recList, token.type) will create new instance of ArrayList and assign it to myFilteredList , but the initial instance provided to your adapter was different您需要通知Adapter对其数据源的更改,在您的情况下是ListmyFilteredList = Gson().fromJson(recList, token.type)将创建ArrayList的新实例并将其分配给myFilteredList ,但提供的初始实例你的适配器是不同的

private val receiverFilteredList = object : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        var recList = intent.getStringExtra("filteredList")
        if (recList != null){
            var token: TypeToken<ArrayList<Model?>?> = object : TypeToken<ArrayList<Model?>?>() {}
            myFilteredList.clear() //clear existing data 
            myFilteredList.addAll(Gson().fromJson(recList, token.type)) // add new data
            adapter.notifyDataSetChanged() //reset the list
        }
    }
}

I had to change approach, broadcast receiver wasn't working for me.我不得不改变方法,广播接收器不适合我。 I sent the list (Gson().toJson) from adapter via Intent.putExtra, and in the activity I received it like this我通过 Intent.putExtra 从适配器发送了列表 (Gson().toJson),并且在活动中我像这样收到了它

recList= intent.getStringExtra("filteredList")

if (recList!= null){
   var token: TypeToken<ArrayList<Model?>?> = object : TypeToken<ArrayList<Model?>?>() {}
            myFilteredList?.clear()
            myFilteredList= Gson().fromJson(recList, token.type)
}

Now everything is working fine.现在一切正常。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM