In my simple application i would like to change one image view url by clicking on button after setting default url into that, for example in imageView i have google logo image and after clicking on button that updating to image stackoverflow logo

unfortunately my implemented code for BindingAdapter that only work once when i run application,showing google logo image and after changing image url and using notifyPropertyChanged that doesn't work

ImageBindingAdapter class:

object ImageBindingAdapter {
    @BindingAdapter("imageUrl", "placeholder", requireAll = false)
    @JvmStatic
    fun loadImage(imageView: ImageView, imageUrl: String, placeholder: Drawable) {
        if (imageUrl != "") {
            Picasso.get().load(imageUrl)
                    .placeholder(R.drawable.image_not_found)
                    .error(R.drawable.image_not_found)
                    .into(imageView)
        } 
    }
}

my User class which that have image url

class User : BaseObservable() {
    var username: String? = null
    var password: String? = null
    var profilePicUrl: String? = null
        @Bindable
        set(profilePicUrl) {
            field = profilePicUrl
            notifyPropertyChanged(BR.profilePicUrl)
        }
}

and then my LoginActivity

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding: LoginActivityBinding = DataBindingUtil.setContentView(this, R.layout.login_activity)
    binding.viewModel = ViewModelProviders.of(this, LoginViewModelFactory(this)).get(LoginViewModel::class.java)

    val user = User()
    user.username = "xxxx"
    user.password = "xxxxxxxxxx"
    user.profilePicUrl = ""

    binding.user = user

    val handlers = ClickHandler()
    handlers.user = user
    binding.handlers = handlers

}

ClickHandler class:

class ClickHandler {
    var user: User? = null

    fun clickOnLoginButton(view: View) {
        user?.profilePicUrl = "https://www.androidhive.info/RxJava/wp-content/uploads/2018/02/1-370x247.jpg"
    }
}

TIP: this ClickHandler after multiple clicking on that work fine, i tested

layout xml

<Button
    ...
    android:onClick="@{handlers.clickOnLoginButton}"
    ... />

<com.mikhaellopez.circularimageview.CircularImageView
    ...
    app:imageUrl="@{user.profilePicUrl}"
    app:placeholder="@{@drawable/img_wizard_1}"
    ... />

0 Answers