How to set one item select with switch button and boolean in Room database MVVM Kotlin Android-ThrowExceptions

Exception or error:

I’ve got profiles List in Recyclerview. I need to make switch button with only one item selected and update boolean “isActive” for every profile.
When switch button is checked boolean for this item should be true, and false for else.

this is my adapter

class ProfilesListAdapter(profileEvents: ProfileEvents) : RecyclerView.Adapter<ProfilesListAdapter.ViewHolder>(), Filterable {

    private var proflieList: List<ProfileEntity> = arrayListOf()
    private var filteredProfileList: List<ProfileEntity> = arrayListOf()
    private val listener: ProfileEvents = profileEvents

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

    override fun getItemCount(): Int = filteredProfileList.size

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(filteredProfileList[position], listener)
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun bind(profileEntity: ProfileEntity, listener: ProfileEvents) {
            itemView.NamePRV.text = profileEntity.userNames
            itemView.LastmanePRV.text = profileEntity.userLastname
            itemView.DatePRV.text = profileEntity.bornDay + profileEntity.bornMonth + profileEntity.bornYear


            itemView.ProfileDeleteButton.setOnClickListener {
                listener.onDeleteClicked(profileEntity)
            }




            itemView.setOnClickListener {
                listener.onViewClicked(profileEntity)
            }
        }
    }
    /**
     * Search Filter implementation
     * */
    override fun getFilter(): Filter {
        return object : Filter() {
            override fun performFiltering(p0: CharSequence?): FilterResults {
                val charString = p0.toString()
                filteredProfileList = if (charString.isEmpty()) {
                    proflieList
                } else {
                    val filteredList = arrayListOf<ProfileEntity>()
                    for (row in proflieList) {
                        if (row.userNames.toLowerCase().contains(charString.toLowerCase())
                                || row.userLastname.contains(charString.toLowerCase())
                                )
                        {
                            filteredList.add(row)
                        }
                    }
                    filteredList
                }

                val filterResults = FilterResults()
                filterResults.values = filteredProfileList
                return filterResults
            }

            override fun publishResults(p0: CharSequence?, p1: FilterResults?) {
                filteredProfileList = p1?.values as List<ProfileEntity>
                notifyDataSetChanged()
            }

        }
    }

    /**
     * Activity uses this method to update todoList with the help of LiveData
     * */
    fun setAllProfilesItems(profileItems: List<ProfileEntity>) {
        this.proflieList = profileItems
        this.filteredProfileList = profileItems
        notifyDataSetChanged()
    }

    /**
     * RecycleView touch event callbacks
     * */
    interface ProfileEvents {
        fun onDeleteClicked(profileEntity: ProfileEntity)
        fun onViewClicked(profileEntity: ProfileEntity)
     //   fun onSwitchClicked(profileEntity: ProfileEntity)
    }

}

And this is my profiles List activity


class ProfilesListActivity : AppCompatActivity(), ProfilesListAdapter.ProfileEvents {

    private lateinit var profilelistViewModel: InAppProfilesViewModel
    private lateinit var searchView: SearchView
    private lateinit var profileListAdapter: ProfilesListAdapter

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(activity_profiles_list)

        //Setting up RecyclerView
        rv_profiles.layoutManager = LinearLayoutManager(this)
        profileListAdapter =
            ProfilesListAdapter(this)
        rv_profiles.adapter = profileListAdapter


        //Setting up ViewModel and LiveData
        profilelistViewModel = ViewModelProvider(this).get(InAppProfilesViewModel::class.java)
        profilelistViewModel.getAllProfilesList().observe(this, Observer {
            profileListAdapter.setAllProfilesItems(it)
        })

        //FAB click listener
        fab_new_todo.setOnClickListener {
            resetSearchView()
            val intent = Intent(this@ProfilesListActivity, NewProfileActivity::class.java)
            startActivityForResult(intent, Constants.INTENT_CREATE_PROFILE)
        }
    }


    //Callback when user clicks on Delete note
    override fun onDeleteClicked(profileEntity: ProfileEntity) {
        val intent = Intent(this, ProfileDetails::class.java)
        startActivity(intent)

      //  profilelistViewModel.deleteProfile(profileEntity)
    }

    //Callback when user clicks on view note
    override fun onViewClicked(profileEntity: ProfileEntity) {
        resetSearchView()

        val intent = Intent(this@ProfilesListActivity, NewProfileActivity::class.java)
       intent.putExtra(Constants.INTENT_OBJECT, profileEntity)
       startActivityForResult(intent, Constants.INTENT_UPDATE_PROFILE)
    }


    /**
     * Activity result callback
     * Triggers when Save button clicked from @CreateTodoActivity
     * */

    @SuppressLint("MissingSuperCall")
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK) {
            val profileEntity = data?.getParcelableExtra<ProfileEntity>(
                Constants.INTENT_OBJECT)!!
            when (requestCode) {
                Constants.INTENT_CREATE_PROFILE -> {
                    profilelistViewModel.saveProfile(profileEntity)
                }
                Constants.INTENT_UPDATE_PROFILE -> {
                    profilelistViewModel.updateProfile(profileEntity)
                }
            }
        }
    }


    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        menuInflater.inflate(R.menu.menu_search, menu)
        val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager
        searchView = menu?.findItem(R.id.search_profile)
                ?.actionView as SearchView
        searchView.setSearchableInfo(searchManager
                .getSearchableInfo(componentName))
        searchView.maxWidth = Integer.MAX_VALUE
        searchView.setOnQueryTextListener(object : SearchView.OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                profileListAdapter.filter.filter(query)
                return false
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                profileListAdapter.filter.filter(newText)
                return false
            }

        })
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        return when (item?.itemId) {
            R.id.search_profile -> true
            else -> super.onOptionsItemSelected(item)
        }
    }

    override fun onBackPressed() {
       resetSearchView()
        super.onBackPressed()
    }

   private fun resetSearchView() {
        if (!searchView.isIconified) {
            searchView.isIconified = true
            return
        }
    }
}

How to do this?

How to solve:

Leave a Reply

Your email address will not be published. Required fields are marked *