mafortis
mafortis

Reputation: 7128

Kotlin adapter Required: Int Found: String?

I have API data which ID's are uuid (strings) and not (integer) and when I want to get those ids in my adapter it says

Type mismatch.
Required:
Int
Found:
String?

Sample of API items

{
    "id":"0ade1bfb-6d02-4a1f-9cd4-dc88fa8aadbd",
    "name":"ABC",
    "photo":null // if not null, will be full URL of image (https://example.com/img/abc.jpg)
}

Code

Adapter (commented)

class ServicesAdapter(private val serviceList: List<Service>) : RecyclerView.Adapter<ServicesAdapter.ServiceViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ServiceViewHolder {
        val imageView =
            LayoutInflater.from(parent.context).inflate(R.layout.service_item, parent, false)

        return ServiceViewHolder(imageView)
    }

    override fun onBindViewHolder(holder: ServiceViewHolder, position: Int) {
        val currentItem = serviceList[position]

        holder.imageView.setImageResource(currentItem.photo) <-- error line
        holder.textView.text = currentItem.name
    }

    override fun getItemCount() = serviceList.size

    class ServiceViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView = itemView.imageView
        val textView: TextView = itemView.textView2
    }
}

Activity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    run("api_url")
}

fun run(url: String) {
    val request = Request.Builder()
        .url(url)
        .build()

    client.newCall(request).enqueue(object : Callback {
        override fun onFailure(call: Call, e: IOException) {}
        override fun onResponse(call: Call, response: Response) {
            val list: ArrayList<Service> = ArrayList()
            getServices(response.body()!!.string(), list)

            recycler.layoutManager = LinearLayoutManager(this@MainActivity)
            recycler.adapter = ServicesAdapter(list)
        }
    })
}

fun getServices(response: String, list: ArrayList<Service>) {
    var jsonObject = JSONObject(response)
    val jsonArray = jsonObject.getJSONArray("data")

    for (i in 0 until jsonArray.length()) {
        val jsonObject1 = jsonArray.getJSONObject(i)
        var listingObject = Service(
            jsonObject1.getString("id"),
            jsonObject1.getString("name"),
            jsonObject1.getString("photo")
        )
        list.add(listingObject)
    }
}

Class

class Service (val id: String?, val name: String?, val photo: String?) {
}

Any idea?

Upvotes: 0

Views: 740

Answers (3)

Dhanuesh
Dhanuesh

Reputation: 1596

Add the following lines of code in your OnBindViewHolder to load images from the URL

       currentItem.photo?.apply{
        Glide.with(holder.imageView.context)
        .load(this)
        .into(holder.imageView)
   }

Upvotes: 1

Priyanka
Priyanka

Reputation: 1875

Just set image when you are getting it in response i.e. its value is not empty or null.Also you need to use any library to set image to imageview for example You can use Square's Picasso and can update your code as below

 if(!TextUtils.isEmpty(currentItem.photo))
     Picasso
     .get()
     .load(currentItem.photo)
     .into(holder.imageView)
    

Upvotes: 0

Shashank
Shashank

Reputation: 96

holder.imageView.setImageResource(currentItem.photo)

this method requires int value, which you are providing null(treated as null string)

try replacing null as blank while parsing json data

or you can use this

public static boolean isBlankOrNull(String value)
{
    return (value == null || value.equals("") || value.equals("null") || value.trim().equals(""));
}

like this

holder.imageView.setImageResource(isBlankOrNull(currentItem.photo) ? 0 : currentItem.photo)

Upvotes: 0

Related Questions