0

basically i have a button -->Onclick-->alert dialog is appearing ..in that alert dialog im having a ratingbar.on that rating bar im sending the rate value to server using retrofit which i have done succesfully.but what i have problem is storing that rate value using shared preference.im sharing my code following:--

ratebutton.setOnClickListener {
                    val mBuild: AlertDialog.Builder = AlertDialog.Builder(this@Product_details)
                    val mView: View = layoutInflater.inflate(R.layout.ratedialog, null)

                    val ratebar =mView.findViewById(R.id.ratingBaruser) as RatingBar

                    val titleimage=mView.findViewById<TextView>(R.id.titleimage)  as TextView
                    val imagerate=mView.findViewById<ImageView>(R.id.imagerate) as ImageView
                    titleimage.setText(ygd)
                 Glide.with(getApplicationContext()).load(res?.body()!!.data.product_images.get(0).image).into(imagerate);
                  val  wmbPreference1 = PreferenceManager.getDefaultSharedPreferences(applicationContext)
                    val rating: Float = wmbPreference1.getFloat("numStars", 0f)
                    ratebar.setRating(rating)
                    ratebar.onRatingBarChangeListener =
                        OnRatingBarChangeListener { ratingBar, rating1, fromUser ->
                         val   editor = wmbPreference1.edit();
                            editor.putFloat("numStars", rating);
                            editor.commit();
                            rateValue = rating1
                            Toast.makeText(this@Product_details, "" + rating1, Toast.LENGTH_SHORT).show()
                        }

                    val btnSubmit =
                        mView.findViewById(R.id.btnSubRating) as Button
                    btnSubmit.setOnClickListener(object : View.OnClickListener {
                        override  fun onClick(v: View?) {
                            RetrofitClient.instancetable.setRating(id,rateValue)
                                .enqueue(object : Callback<Rate_Response> {
                                    override fun onFailure(call: Call<Rate_Response>, t: Throwable) {
                                        Log.d("res", "" + t)
                                    }
                                    override fun onResponse(
                                        call: Call<Rate_Response>,
                                        response: Response<Rate_Response>
                                    ) {
                                        var res = response
                                        if (res.isSuccessful) {
                                            Toast.makeText(
                                                applicationContext,
                                                res.body()?.user_msg,
                                                Toast.LENGTH_LONG
                                            ).show()
                                        }
                                        else{
                                            try {
                                                val jObjError =
                                                    JSONObject(response.errorBody()!!.string())
                                                Toast.makeText(
                                                    applicationContext,
                                                    jObjError.getString("message")+jObjError.getString("user_msg"),
                                                    Toast.LENGTH_LONG
                                                ).show()
                                            } catch (e: Exception) {
                                                Toast.makeText(applicationContext, e.message, Toast.LENGTH_LONG).show()
                                                Log.e("errorrr",e.message)
                                            }
                                        }
                                    }
                                })
                            Toast.makeText(this@Product_details, "" + rateValue, Toast.LENGTH_SHORT).show()
                        }
                    })

                    mBuild.setView(mView)
                    val dialog: AlertDialog = mBuild.create()
                    dialog.show()
                }

Above code output:--

when i again click that button i unable to see the previous rate value done by user

i need help thanks in advance

android dev
  • 200
  • 5
  • 18

0 Answers0