0

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>
2
  • why are you putting generics in your Activity? Commented May 29, 2017 at 12:52
  • because its use for multiple list using different model Commented May 29, 2017 at 13:31

1 Answer 1

1

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

2 Comments

thanx bro , can you tell me how i used in xml when generic typr just like this
i have one quiestion also , please check i edited quiestion

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.