Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use method of object in Android data binding?

Suppose I have this object:

public class Field
{
   List<String> list;
   public Field()
   {
     list = new ArrayList();
   }
   public boolean isOnTheList(String someText)
   {
     return list.contains(someText);
   }
}

Now I want to use this function on a xml with binding like this.

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

    <import type="android.view.View"/>

    <variable
        name="field"
        type="com.greendao.db.Field"/>        

    <variable
        name="user"
        type="com.package.User"/>
</data>

<LinearLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"    
    android:visibility="@{field.isOnTheList(user.name)?View.VISIBLE:View.GONE}"
    tools:context=".advisor.new_visit_report.activities.FieldsSelectionActivity"> ...

The problem is that is not working. Anyone has already tried this?

like image 359
Dan Ponce Avatar asked Aug 23 '16 17:08

Dan Ponce


1 Answers

If everything else is set correctly, I would suggest to edit your method to return View.VISIBLE or View.GONE directly:

public int isOnTheList(String someText) {
    return list.contains(someText) ? View.VISIBLE : View.GONE;
}

Used in xml:

android:visibility="@{field.isOnTheList(user.name)}
like image 195
yennsarah Avatar answered Oct 12 '22 16:10

yennsarah