Search code examples
javaandroidkotlindata-bindingsharedpreferences

useing shared preferences to save Button click state


i am working on comment section of an application like play store using mvvm,coroutine,kodein and DataBinding.i set login page in my main activity After the user presses the login button Comment Activity will become apparent.

I set recyclerview in comment activity. at end of every item i asked a question that was this review helpful or not. if not press no else press yes.i used shared preferences to save button state. for example when i clicked on yes button yesclicked(boolean variable) will be saved in sharedpref. till here everything works fine.

the problem--> i saved that state then i getYesButtonState() in onBindViewHolder method of recyclerview class and i said when activity recreated change the background color of yes button to #D5FFD7 for this purpose that you clicked on this before

but nothing happend and it didnt work

this code is for shared pref

 val SPP_NAME = "ButtonState"
var buttonLocalState: SharedPreferences = context.getSharedPreferences(SPP_NAME, Context.MODE_PRIVATE)

fun setYesButtonState(isClicked: Boolean) {
    val userLocalDatabaseEditor: SharedPreferences.Editor = buttonLocalState.edit()
    userLocalDatabaseEditor.putBoolean("yesClicked", isClicked)
    userLocalDatabaseEditor.apply()
}

fun setNoButtonState(isClicked: Boolean) {
    val userLocalDatabaseEditor: SharedPreferences.Editor = buttonLocalState.edit()
    userLocalDatabaseEditor.putBoolean("noClicked", isClicked)
    userLocalDatabaseEditor.apply()
}

fun getYesButtonState(): Boolean? {
    if (buttonLocalState.getBoolean("yesClicked", false) == false) {
        return null
    } else {
        return true
    }

}

fun getNoButtonState(): Boolean? {
    if (buttonLocalState.getBoolean("noClicked", false) == false) {
        return null
    } else {
        return true
    }
}

fun clearButtonState() {
    val userLocalDatabaseEditor: SharedPreferences.Editor = buttonLocalState.edit()
    userLocalDatabaseEditor.clear()
    userLocalDatabaseEditor.apply()
}

this is for recyclerview class:

        //yes button clicked
        yesbtn.setOnClickListener {
            yesClicked = true
            localStore.clearButtonState()
            localStore.setYesButtonState(yesClicked)
            val Helpful = 1

            if (localStore.getYesButtonState() == true) {
                nobtn.setBackgroundColor(Color.WHITE)
                startColorAnimation(yesbtn)
                activity.handler.postDelayed({
                    yesbtn.setBackgroundColor(Color.parseColor("#D5FFD7"))
                    nobtn.isClickable = true
                }, 892.25.toLong())
            }

            try {
                viewModel.deleteperson(
                    localStore.getUserName().toString(),
                    currentItem.id
                ).observe(mlifecycleOwner, Observer {

                })
            } catch (e: IllegalStateException) {
                e.fillInStackTrace()
            }


            viewModel.feedback(
                currentItem.id,
                localStore.getUserName().toString(),
                currentItem.description,
                Helpful
            ).observe(mlifecycleOwner, Observer {

            })
            Toast.makeText(
                context,
                "thanks for your feedback",
                Toast.LENGTH_SHORT
            ).show()
            yesbtn.isClickable = false

        }


        //no button clicked
        nobtn.setOnClickListener {
            noClicked = true
            localStore.clearButtonState()
            localStore.setNoButtonState(noClicked)
            val Helpful = 0


            if (localStore.getNoButtonState() == true) {
                yesbtn.setBackgroundColor(Color.WHITE)
                startColorAnimation(nobtn)
                activity.handler.postDelayed({
                    nobtn.setBackgroundColor(Color.parseColor("#D5FFD7"))
                    yesbtn.isClickable = true
                }, 892.25.toLong())
            }



            try {
                viewModel.deleteperson(
                    localStore.getUserName().toString(),
                    currentItem.id
                ).observe(mlifecycleOwner, Observer {

                })
            } catch (e: IllegalStateException) {
                e.fillInStackTrace()
            }
            viewModel.feedback(
                currentItem.id,
                localStore.getUserName().toString(),
                currentItem.description,
                Helpful
            ).observe(mlifecycleOwner, Observer {

            })
            Toast.makeText(
                context,
                "please tell us why",
                Toast.LENGTH_SHORT
            ).show()
            it.isClickable = false
        }

where i did wrong. thanks for your Help


Solution

  • try this after the button click .. call notifyDataSetChanged(); in your adapter class