2016-10-14 3 views
2

データバインディングを使用して、オブジェクトの文字列フィールド値をXMLファイルの別の文字列値と比較するにはどうすればよいですか?それはxmlファイルでそうすることが可能ですか、私は@BindingAdapter注釈で私のプロジェクトのどこかにメソッドを作成する必要がありますか? 以下は私がこれまでに試したことであり、うまくいきませんでした。また、ハードコードされた文字列値ではなく、文字列リソース値と比較すると良いでしょう。データバインディングを使用してxmlファイルの文字列を比較する方法

<RadioGroup 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <RadioButton 
       android:id="@+id/male" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:checked="@{user.gender.equalsIgnoreCase("male")}" 
       android:text="@string/male"/> 

      <RadioButton 
       android:id="@+id/female" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:checked="@{user.gender.equalsIgnoreCase("female")}" 
       android:text="@string/female"/> 

     </RadioGroup> 

ありがとうございました。

答えて

8

あなたはそれがほぼ正しいです。

 <RadioButton 
      android:id="@+id/male" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checked="@{user.gender.equalsIgnoreCase(`male`)}" 
      android:text="@string/male"/> 

あなたは、文字列と一緒に、単一引用符で文字定数を混在させることができます:文字列定数は、式のバック引用符を使用してXMLで二重引用符内に二重引用符、サポートを結合ので、アンドロイドのデータを使用することはできません定数。

XMLでは、属性値に一重引用符を使用できるため、式内で二重引用符を使用できます。これは、より一般的なアプローチである:

 <RadioButton 
      android:id="@+id/female" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checked='@{user.gender.equalsIgnoreCase("female")}' 
      android:text="@string/female"/> 

あなたは全部をスキップして、文字列リソースまたは定数を使用することができます。

 <RadioButton 
      android:id="@+id/male" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checked="@{user.gender.equalsIgnoreCase(@string/male)}" 
      android:text="@string/male"/> 

     <RadioButton 
      android:id="@+id/female" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:checked="@{user.gender.equalsIgnoreCase(StringConstants.FEMALE)}" 
      android:text="@string/female"/> 
関連する問題