ratingbar does not filled the float value coming from JSON android

我怕爱的太早我们不能终老 提交于 2020-07-31 04:42:31

问题


the thing is if rating is 3.5 then ratingbar fills upto 3 star ...it doesnt filled half of the fourth star....

I dont know where im going wrong Need help thanks:Following is my code

rating:

    <RatingBar
        android:numStars="5"
        android:id="@+id/ratingbar"
        android:layout_width="wrap_content"
        android:clickable="false"
        android:isIndicator="true"
        android:layout_height="45dp"
        android:focusableInTouchMode="false"
        android:defaultFocusHighlightEnabled="false"
        android:focusable="false"
        android:progressBackgroundTint="#7F7F7F"
        android:progressTint="#FFBA00"
        android:stepSize="0.5"
      />

here is my adapter:-not posting full code just an important part

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {

    holder.productname.text = Tablelist.get(position).name
    holder.producername.text = Tablelist.get(position).producer

    holder.productprice.text = Tablelist.get(position).cost.toString()

    Glide.with(context).load(Tablelist.get(position).product_images)
        .into(holder.image)
    holder.rate.setRating(Tablelist.get(position).rating);
 //   holder.rate.setStepSize(Tablelist.get(position).rating);// to show to stars


    holder.itemView!!.setOnClickListener {


        val context:Context=holder.itemView.context
        val i=Intent(context,
            Product_details::class.java)
        i.putExtra("id",Tablelist.get(position).id.toString())
      Log.e("checkid",Tablelist.get(position).id.toString())
        context.startActivity(i)
    }
}

fun setMovieListItems(movieList: List<Table_data>){
    this.Tablelist = movieList;
    notifyDataSetChanged()
}

class MyViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView!!) {

    val productname: TextView = itemView!!.findViewById(R.id.title)
    val producername: TextView = itemView!!.findViewById(R.id.title1)
    val productprice: TextView = itemView!!.findViewById(R.id.title2)
    val rate: RatingBar=itemView!!.findViewById(R.id.ratingbar)

    val image: ImageView = itemView!!.findViewById(R.id.image)

}

here is my Tablelist:

data class Table_data (

@SerializedName("id") val id : Int,
@SerializedName("product_category_id") val product_category_id : Int,
@SerializedName("name") val name : String,
@SerializedName("producer") val producer : String,
@SerializedName("description") val description : String,
@SerializedName("cost") val cost : Int,
@SerializedName("rating") val rating : Float=0.0f,
@SerializedName("view_count") val view_count : Int,
@SerializedName("created") val created : String,
@SerializedName("modified") val modified : String,
@SerializedName("product_images") val product_images : String
)

回答1:


Not sure but here problem might be with the float conversion from json adapter. what you can do is get the field as String, so the value is not lost at the time of conversion.

@SerializedName("rating") val rating : String,

now in your adapter try to convert this to float.

 holder.rate.setRating(Tablelist.get(position).rating.toFloat())

can you try this one.



来源:https://stackoverflow.com/questions/63170412/ratingbar-does-not-filled-the-float-value-coming-from-json-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!