vyankatesh jadhav
vyankatesh jadhav

Reputation: 23

android data bindings using generics java

i am newbiew in an android databinding,i wanna use generics type in an databinding layout , is it possible or not please tell me?

JAVA activity

public class SearchableActivity<R> extends ErpActivity<R extends MasterPojo>
{

  public  void callToBack(R r){
  } 
}

XML layout

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

<data>

    <variable
        name="task"
        type="R extends Masterpojo" />

    <variable
        name="activity"
        type="com.cloud9.erp.activities.SearchableActivity<R>" />


</data>

<TextView
    style="@style/valued_style"
    android:onClick="@{() -> activity.callToBack(task)}"
    android:text="Erp"
    android:textColor="@android:color/black"
    android:textSize="@dimen/fab_margin" />
</layout>

Upvotes: 0

Views: 1363

Answers (1)

Luiz Fernando Salvaterra
Luiz Fernando Salvaterra

Reputation: 4182

Yes, it`s possible doing a scape for your generic type:

<data>
    <variable
        name="activity"
        type="com.cloud9.erp.activities.SearchableActivity&lt;R>" />
</data>

Keep in mind that the Android Studio will still show a "Cannot resolve symbol" error, but your layout file will compile.

Also, this is not the best implementation to do that, you should use Handlers for this kind of operations:

public class MyHandlers {
    public void onClickFriend(View view) { ... }
}

And then, use it in your XML:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="handlers" type="com.example.Handlers"/>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"
           android:onClick="@{handlers::onClickFriend}"/>
   </LinearLayout>
</layout>

Upvotes: 1

Related Questions