Wednesday, 28 October 2020

Spinner value using retrofit and viewmodel

Im trying to implement editting cart in my project..for that i have used spinner

i posted two methods with using viewmodel and one is without using viewmodel

without using viewmodel works fine the problem left with using viewmodel without

using viewmodel works fine <-- it posts correct value in call ,setting properly value in spinner when i reviisit cart

with using viewmodel <-- if i set spinner as 3 it shows sucess but when i revisit i see 8 in spinner and also postman i see 8 in response

Adapter::----------

var country =
    arrayOf(1, 2, 3, 4, 5, 6, 7, 8)
   .
   .
   .
      val aa: ArrayAdapter<*> =
        ArrayAdapter<Any?>(context, android.R.layout.simple_spinner_item, country)
    aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
    holder.spin.setAdapter(aa)
      holder.spin.setSelection(dataList?.get(position)?.quantity!! - 1, false)
    holder.spin.setOnItemSelectedListener(object : OnItemSelectedListener {
        override fun onItemSelected(
            parent: AdapterView<*>,
            view: View,
            position1: Int,
            id: Long
        ) {
            progressDialog.show()
            val id = dataList?.get(position)!!.product.id
            itemClick?.EditItem(position1,id!!.toInt())
        }
        override fun onNothingSelected(parent: AdapterView<*>?) {
            // todo for nothing selected
        }
    })

Interface::-----

interface OnItemClick {
fun DeleteItem(position: Int, id:Int)
fun EditItem(position: Int,id: Int)
}

Activity::----

    var country =
    arrayOf(1, 2, 3, 4, 5, 6, 7, 8)
 .
  .
  .
    override fun EditItem(position: Int, id: Int) {
    val token: String =
        SharedPrefManager.getInstance(
            applicationContext
        ).user.access_token.toString()
    RetrofitClient.instance.editCart(token, id, arrayOf(country[position]))
        .enqueue(object : Callback<DeleteResponse> {
            override fun onFailure(call: Call<DeleteResponse>, t: Throwable) {
                Log.d("res", "" + t)
            }
            override fun onResponse(
                call: Call<DeleteResponse>,
                response: Response<DeleteResponse>
            ) {
                progressDialog?.dismiss()
                finish()
                var res = response
                if (res.isSuccessful) {
                    Toast.makeText(
                        applicationContext,
                        res.body()?.user_msg,
                        Toast.LENGTH_LONG
                    ).show()
                    progressDialog?.dismiss()
                    val intent =
                        Intent(applicationContext, AddToCart::class.java)
                    intent.flags =
                        Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK
                    applicationContext.startActivity(intent)
                    overridePendingTransition(0, 0)
                    Log.d("kjsfgxhufb", response.body()?.user_msg.toString())
                } 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)
                    }
                }
            }
        })
}

Api::--------

   @FormUrlEncoded
   @POST("editCart")
   fun editCart(
    @Header("access_token") token: String,
    @Field("product_id") product_id: Int,
    @Field("quantity") quantity: Array<Int>
)
        :Call<DeleteResponse>

Above code is working without an implementation of viewmodel....it is getting edited and got success response

with viewmodel implementation

viewmodel::-------

    var country =
    arrayOf(1, 2, 3, 4, 5, 6, 7, 8)
    .
     .
       .
       fun loadEdit(){
    val id = product_id.value!!.toString().trim()
    val token: String =
        SharedPrefManager.getInstance(
            getApplication()
        ).user.access_token.toString()
    RetrofitClient.instance.editCart(token, id.toInt(), country)
        .enqueue(object : Callback<DeleteResponse> {
            override fun onFailure(call: Call<DeleteResponse>, t: Throwable) {
                Log.d("res", "" + t)
            }
            override fun onResponse(
                call: Call<DeleteResponse>,
                response: Response<DeleteResponse>
            ) {
               var res = response
                if (res.isSuccessful) {
                    Toast.makeText(
                        getApplication(),
                        res.body()?.user_msg,
                        Toast.LENGTH_LONG
                    ).show()
                
                } else {
                    try {
                        val jObjError =
                            JSONObject(response.errorBody()!!.string())
                        Toast.makeText(
                            getApplication(),
                            jObjError.getString("message") + jObjError.getString("user_msg"),
                            Toast.LENGTH_LONG
                        ).show()
                    } catch (e: Exception) {
                        Toast.makeText(getApplication(), e.message, Toast.LENGTH_LONG).show()
                        Log.e("errorrr", e.message)
                    }
                }
            }
        })
      }

    

Activity::-----

      var country =
    arrayOf(1, 2, 3, 4, 5, 6, 7, 8)
    .
    .
    .
    override fun EditItem(position: Int, id: Int) {
    viewModel.product_id.value =id.toString()
    viewModel.loadEdit()
    viewModel.country[position]=country[position]
    viewModel.Results.observe(this) { result ->
        when (result) {
            CartViewModel.Result.Missing -> {
                Toast.makeText(
                    applicationContext, "product is missing", Toast.LENGTH_LONG
                ).show()
            }
            CartViewModel.Result.Success -> {
                finish()
                val intent =
                    Intent(applicationContext, AddToCart::class.java)
                intent.flags =
                    Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK
                startActivity(intent)
                overridePendingTransition(0, 0)                }
            else ->
            {
                Toast.makeText(applicationContext,"hello",Toast.LENGTH_LONG).show()
            }
        }
    }

}

From implementation of viewmodel im getting an success response in toast that successfully updated cart but when i revisit the cart again im seeing value as 8 in spinner

example if i set spinner as 3 it shows sucess but when i revisit i see 8 in spinner and also postman i see 8 in response :(

need help in spinner to set and send value to call thanks



from Spinner value using retrofit and viewmodel

No comments:

Post a Comment