Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android databinding and ternary operator (?:) to get drawables

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

like image 405
Jemil Riahi Avatar asked Jul 16 '16 23:07

Jemil Riahi


People also ask

Can I use both DataBinding and ViewBinding?

View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.

Is Android DataBinding deprecated?

Recently Android has announced that with Kotlin 1.4. 20, their Android Kotlin Extensions Gradle plugin will be deprecated and will no longer be shipped in the future Kotlin releases. Android Kotlin Extensions plugin brought with it two very cool features : Synthetics let you replace calls to findViewById with kotlinx.

How do I compare strings in DataBinding?

There is no need to import the String class into your layout file. To check whether two strings have equal value or not equals() method should be used. = is used to check the whether the two strings refer to the same reference object or not.


2 Answers

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}"

like image 136
yigit Avatar answered Oct 02 '22 16:10

yigit


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"/>
like image 31
Jemil Riahi Avatar answered Oct 02 '22 16:10

Jemil Riahi