6

I am trying to get drawables depending on if a function is false or true. But i am doing something wrong. So any help is appreciated

            <ImageView
              android:layout_width="24dp"
              android:layout_height="24dp"
              android:onClick="@{() -> handler.onVoteClicked(gameFeed.uid)}"
              android:src="@{() -> handler.existInList(feed.ratings,feed.uid) ? @drawable/ic_star_border : @drawable/ic_star}"
              android:layout_gravity="center_vertical"/>

This is the function i am trying to call from my handler.

  public boolean existInList(Map<String,Boolean> ratings, String userId) {
    for (Map.Entry<String, Boolean> entry : ratings.entrySet()) {
      if(entry.getValue().equals(userId)) {
       return true;
      }
    }
    return false;
  }

Added error that i got

Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
  ****/ data binding error ****msg:Cannot find the proper callback class 
for android:src. Tried android.graphics.drawable.Drawable but it has 4 abstract methods, should have 1 abstract methods. file:/Users/brahim/ProjectNinjo/app/src/main/res/layout/adapter_feed_row.xml loc:108:33 - 108:135 ****\ data binding error ****

Update

What i noticed is that it can grab the drawable. But it has problems calling that function. Im guessing that i cant call a function from a ternary operator or i am calling the function in a bad way

Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38

2 Answers2

19

You are using the wrong syntax. () -> ... is the callback syntax, which is executed when an event happens.

Example: android:onClick="@() -> callback.onClick()".

Instead, you just want a simple binding expression like this:

android:src="@{handler.existInList(feed.ratings,feed.uid) ? @drawable/ic_star_border : @drawable/ic_star}"

yigit
  • 37,683
  • 13
  • 72
  • 58
  • Breaks with *android.content.res.Resources$NotFoundException: File res/drawable/ic_retry.xml from drawable resource ID #0x7f0700b5* on **SM-G630H KitKat 4.4.4 API 19**. – iCantC Jan 20 '20 at 12:45
  • @iCantC That just seems like the age-old issue of "file is not actually there(we always think it's there)" – Hiro Nov 22 '20 at 21:11
  • Always find myself coming back here, because the IDE does not give any hint if what I am doing is correct until the entire ternary operator is written and compiled. What's worst, you can actually import the R folder to the XML, and place the int value of the resource in the operator, but the image comes out bugged for some reason. – Delark Jul 09 '21 at 23:55
1

This solved it. Instead of calling a function on the handler class. I found out that an hashmap have contains functions. So that solved it

            <ImageView
              android:layout_width="24dp"
              android:layout_height="24dp"
              android:onClick="@{() -> handler.onVoteClicked(feed.uid)}"
              android:src="@{feed.ratings.containsKey(feed.uid) ? @drawable/ic_star : @drawable/ic_star_border}"
              android:layout_gravity="center_vertical"/>
Jemil Riahi
  • 1,360
  • 5
  • 18
  • 38