简体   繁体   中英

ratingbar does not filled the float value coming from JSON android

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
)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM