2015-11-24 10 views
5

So verwende ich dieses populären Daten Code-Schnipsel Bindungselemente laden in Bild in Imageview der Liste in URL übergeben:Wie greife ich auf eine Instanzvariable in einem BindingAdapter zu, wenn ich die Android-Datenbindung verwende?

<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="150dp"" 
     app:imageUrl="@{movie.imageUrl}" 
     /> 

The Binding Adapter:

class Movie{ 
    boolean isLoaded; 

    @BindingAdapter({"bind:imageUrl"}) 
     public static void loadImage(final ImageView view, String imageUrl) { 

      Picasso.with(view.getContext()) 
       .load(imageUrl) 
       .into(view, new Callback.EmptyCallback() { 
       @Override public void onSuccess() { 
        //set isLoaded to true for the listview item 
        // but cannot access boolean isLoaded as it is non static. 
       }); 
    } 

Wenn ich einfach das machen BindingAdapter nicht-statische dann wirft es Fehler:

java.lang.IllegalStateException: Required DataBindingComponent is null  in class MovieTileBinding. A BindingAdapter in  com.example.moviesapp.Pojos.Results is not static and requires an object to  use, retrieved from the DataBindingComponent. If you don't use an inflation method taking a DataBindingComponent, use DataBindingUtil.setDefaultComponent or make all BindingAdapter methods static. 
+1

Dieser große Blogpost wird zeigen, u, wie nicht-statische verwenden BindingAdapter https://realm.io/news/data-binding-android-boyar-mount/ . –

+0

Überprüfen Sie die Lösung von dieser doppelten Frage: http://stackoverflow.com/a/38216344/1650674 – tir38

Antwort

1

Put ganzen Film instace durch individuelle Abwartens Adapter:

<ImageView 
     android:layout_width="match_parent" 
     android:layout_height="150dp"" 
     app:movie="@{movie}" 
     /> 

The Binding Adapter:

class Movie{ 
boolean isLoaded; 

@BindingAdapter({"movie"}) 
    public static void loadImage(final ImageView view, final Movie movie) { 
     if(movie != null && moview.getImageUrl() != null){ 
      Picasso.with(view.getContext()) 
      .load(movie.getImageUrl()) 
      .into(view, new Callback.EmptyCallback() { 
      @Override public void onSuccess() { 
       image.setLoaded(true); 
      }); 
    } 
} 
Verwandte Themen