0

Hi I'm try to update imageview with databinding but i cent get how to do it right.

my var

@BindingAdapter({"bind:someImage"})
        public  void loadIt(ImageView view, String imageUrl) {
            Picasso.with(view.getContext())
                    .load(getSomeImage())
                    .placeholder(android.R.drawable.alert_dark_frame)
                    .into(view);
}

 public void setSomeImage(){
        notifyPropertyChanged(BR.someImage);
    }

my layout

<ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imagePath"
      app:imageUrl="@{var.someImage}"/>

I have search but can't find solution.... links = link1 , Link2 , Link 3

Community
  • 1
  • 1
Omri Lugasi
  • 337
  • 3
  • 16

3 Answers3

6

you are using wrong property to call Binding method, as you have defined @BindingAdapter({"bind:someImage"}) you need to use someImage while calling it from xml

<ImageView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:id="@+id/imagePath"
  app:someImage="@{var.someImage}"/>
Ravi
  • 34,851
  • 21
  • 122
  • 183
1

Your are not using the same value u have set in the binding adapter, you set @BindingAdapter({"bind:someImage"}) and using app:imageUrl

use this instead:

<ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imagePath"
      bind:someImage="@{var.someImage}"/>

Example:

@BindingAdapter("loadImageUrl")
fun loadImageUrl(imageView: ImageView, url: String) {
    Glide.with(imgView).load(url)
        .placeholder(PLACE_HOLDER_IMAGE)
        .into(imageView)
}

                <ImageView
                    loadImageUrl="@{vm.url}"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
Kareem Alsaifi
  • 399
  • 4
  • 11
  • While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Piotr Labunski Mar 02 '20 at 09:38
0

You can do this without doing anything programmatically if your data model contains the resource ID of the image.

Example:

<layout>

<data>
<import type="android.support.v4.content.ContextCompat" />            
<variable
name="roomInfoItem"
type="com.example......model.RoomInfoModel" /> </data>

<RelativeLayout> 

 <ImageView
    android:id="@+id/iv_room_info_item"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
 />

</RelativeLayout>

</layout>

Just add the following line to your imageView (I wasn't able to format the code when I was adding it there):

android:src="@{ContextCompat.getDrawable(context,roomInfoItem.resId)}"

where resId contains R.drawable.your_image_name

Rohan Taneja
  • 9,687
  • 3
  • 36
  • 48