Rohit Singh
Rohit Singh

Reputation: 18192

How to pass String as a parameter to a method in DataBinding

I am using DataBinding. I have to call a ViewModel method Submit button click. The method has a String parameter. I am accessing the value from EditText.

Here is my method in ViewModel

public void submit(String password){
       // Method definition
}

Here is my layout file

<EditText
    android:id="@+id/password"
    android:hint="Password"
    />
<Button
    android:id="@+id/submitButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="@={() -> viewModel.submit(password.text.toString())}"
/>
 

I am getting this error

error: cannot find symbol import com.rohitksingh.lockbox.databinding.FragmentLoginBindingImpl; ^ symbol: class FragmentLoginBindingImpl

How do I fix this? When I pass an Integer value in the method then it works fine.

Upvotes: 0

Views: 1314

Answers (1)

Ravi Kumar
Ravi Kumar

Reputation: 4508

You cannot access other views inside the lambda. The onClick lambda (from onClickListener) only receives View on which the onClick is called i.e. the current button.

Alternatively, You can use two-way binding with the EditText to store its value inside the view model and access it when the button is pressed.

<EditText
    android:id="@+id/password"
    android:hint="Password"
    android:text="@={viewmodel.password}"
    />

Upvotes: 2

Related Questions