9

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?

6
  • Are you really sure that public class Field is com.greendao.db.Field, instead of com.package.Field? Beyond that, please explain what "not working" means. Are you crashing? If so, what does LogCat show you? Commented Aug 23, 2016 at 18:02
  • Yes, that´s the correct Object. The app is not crashing, it's just that the visibility function is not working Commented Aug 23, 2016 at 18:10
  • are you setting name variable properly? Commented Aug 24, 2016 at 4:18
  • You could try to set the return value of isOnTheList() to int and return View.VISIBLE or View.GONE directly. Commented Sep 1, 2016 at 12:26
  • @RRR Yes, I'm setting them properly Commented Sep 1, 2016 at 14:33

1 Answer 1

3

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)}
Sign up to request clarification or add additional context in comments.

2 Comments

I'll try now and let you know!
I tried with something similar and it works! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.