简体   繁体   中英

custom attribute setter error when data binding - Android

I have custom attribute which is declared as type of string, when a pass the value as just string in xml like this app:cpb_title="sometsring" its working but when i try data binding like this app:cpb_title"@{model.someStringField}" it gaves me error "cannot find setter for "app:cpb_title" that accepts parameter type java.lang.String"

how can i fix it?

attrs.xml

  <declare-styleable name="CircularProgressBar">
    <attr name="cpb_hasShadow" format="boolean"/>
    <attr name="cpb_progressColor" format="string"/>
    <attr name="cpb_backgroundColor" format="string"/>
    <attr name="cpb_title" format="string"/>
    <attr name="cpb_titleColor" format="string"/>
    <attr name="cpb_subtitle" format="string"/>
    <attr name="cpb_subtitleColor" format="string"/>
    <attr name="cpb_strokeWidth" format="integer"/>
</declare-styleable>

inside view class

 String t = a.getString(R.styleable.CircularProgressBar_cpb_title);
    if(t!=null)
        mTitle = t;

You can use BindingAdapter instead of declaring attributes in the attrs.xml file. Please do as below:

class BindingUtil {

    @BindingAdapter("cpb_title")
    public static void setTitle(TextView view, String title) {
        view.setText(title);
    }
}

Then, you can use app:cpb_title attribute in your XML as below:

               <Textview
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cpb_title="@{model.someStringField}" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM