javaandroidkotlincode-conversion

How do i convert kotlin code into java [Android]


This is the code that I'm trying to convert into java but I don't understand it,actually I get this code as an answer but he/she gives me in kotlin

Glide.with(context)
            .load(url)
            .listener(object : RequestListener<Drawable> {
                override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                    //TODO: something on exception
                }
                override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                    Log.d(TAG, "OnResourceReady")
                    dummyShimmerView.visibility = View.GONE
                    postImageView.visibility = View.VISIBLE
                    return false
                }
            })
            .into(imgView)

Solution

  • I think what you are getting confused at is the RequestListener part. Kotlin uses the object notation for implementation of interfaces. So your code will translate to this roughly

     Glide.with(context).load(...)
                    .listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
              //TODO
              return false;
            }
            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
              // Todo
              return false;
            }
          })
    
                    }).into(imgView);