0

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?

Wahdat Jan
  • 3,988
  • 3
  • 21
  • 46
mafortis
  • 6,750
  • 23
  • 130
  • 288

3 Answers3

1

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)
   }
Sekiro
  • 1,537
  • 1
  • 7
  • 21
  • I am getting this error now `android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.` which is comming from this line of my activity file `recycler.layoutManager = LinearLayoutManager(this@MainActivity)` – mafortis Jan 21 '21 at 13:35
  • @mafortis try moving `recycler.layoutManager = LinearLayoutManager(this@MainActivity)` out of the thread. Add it in the XML itself or after `setContentView` – Sekiro Jan 21 '21 at 13:37
  • I've moved it to `onCreate` error is the same – mafortis Jan 21 '21 at 13:38
  • well as a last resort, add it in xml – Sekiro Jan 21 '21 at 13:38
  • sorry I'm newbie, how? – mafortis Jan 21 '21 at 13:39
  • `app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"` add this attribute in your `RecyclerView` – Sekiro Jan 21 '21 at 13:41
  • not 100% sure but I guess it's related to `recycler.` because when I moved it to `onCreate` error was the same but this time pointed to `recycler.adapter = ServicesAdapter(list)` (same goes when I added it to xml) – mafortis Jan 21 '21 at 13:44
  • check out this answer https://stackoverflow.com/questions/5161951/android-only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-vi – Sekiro Jan 21 '21 at 13:47
  • didn't understand it :/ – mafortis Jan 21 '21 at 13:50
  • I added this recycler to my MainActivity file do we have more `main thread` in there? – mafortis Jan 21 '21 at 13:51
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227638/discussion-between-rcs-and-mafortis). – Sekiro Jan 21 '21 at 13:53
0
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)
Shashank
  • 111
  • 6
0

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)
    
Priyanka
  • 1,791
  • 1
  • 7
  • 12