janishar / android-kotlin-mvp-architecture

This repository contains a detailed sample app that implements MVP architecture in Kotlin using Dagger2, Room, RxJava2, FastAndroidNetworking and PlaceholderView
Apache License 2.0
699 stars 193 forks source link

CalledFromWrongThreadException #16

Closed winnerawan closed 6 years ago

winnerawan commented 6 years ago

hi @amitshekhariitbhu i got exception the effect of this exception is list on the recyclerview difficult to show, i must open close app again and again for showing list

Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
        at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7271)
        at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:1127)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.view.View.requestLayout(View.java:19902)
        at android.support.v7.widget.RecyclerView.requestLayout(RecyclerView.java:4090)
        at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5182)
        at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11785)
        at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6961)
        at id.co.next_innovation.okfood.ui.home.view.group_menu.GroupMenuAdapter.addGroupMenuToList$app_debug(GroupMenuAdapter.kt:34)
        at id.co.next_innovation.okfood.ui.home.view.HomeActivity.showGroupMenu(HomeActivity.kt:94)
        at id.co.next_innovation.okfood.ui.home.presenter.HomePresenter$onViewPrepared$$inlined$let$lambda$1.accept(HomePresenter.kt:31)
        at id.co.next_innovation.okfood.ui.home.presenter.HomePresenter$onViewPrepared$$inlined$let$lambda$1.accept(HomePresenter.kt:17)

here my code

override fun onViewPrepared() {
        getView()?.showProgress()
        interactor?.let {
            it.getGroupMenu()
                    .compose(schedulerProvider.ioToMainObservableScheduler())
                    .subscribe({ groupMenuResponse ->
                        getView()?.let {
                            it.hideProgress()
                            it.showGroupMenu(groupMenuResponse.data)
                        }
                    },  { err -> Timber.e(err) })

            it.getType()
                    .compose(schedulerProvider.ioToMainObservableScheduler())
                    .subscribe({ typeResponse ->
                        getView()?.let {
                            it.showType(typeResponse.data)
                        }
                    })
        }
    }
class GroupMenuAdapter(private val groupMenuList: MutableList<GroupMenu>) : RecyclerView.Adapter<GroupMenuAdapter.GroupMenuViewHolder>() {

    override fun getItemCount() = this.groupMenuList.size

    override fun onBindViewHolder(holder: GroupMenuViewHolder, position: Int) = holder.let {
        it.clear()
        it.onBind(position)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = GroupMenuViewHolder(LayoutInflater.from(parent.context).inflate(
            R.layout.adapter_group_menu, parent, false))

    internal fun addGroupMenuToList(menus: List<GroupMenu>) {
        this.groupMenuList.addAll(menus)
        notifyDataSetChanged()
    }

    inner class GroupMenuViewHolder(view: View) : RecyclerView.ViewHolder(view) {

        fun clear() {
            itemView.group_menu_icon.setImageDrawable(null)
            itemView.group_menu_name.text = ""
        }

        fun onBind(position: Int) {

            val (id, name, icon) = groupMenuList[position]

            inflateData(id, name, icon)
            //setItemClickListener(blogUrl)
        }

        private fun inflateData(id: Int?, name: String?, icon: String?) {
            name?.let { itemView.group_menu_name.text = it }
            icon?.let {
                itemView.group_menu_icon.loadImage(it)
            }
        }
    }

}

    override fun showGroupMenu(groupMenus: DataGroupMenu?) = groupMenus?.let {
        groupMenuAdapter.addGroupMenuToList(it.groupMenu)
    }