Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick in Button do not work use Data Binding

I started to learn Data Binding Library https://developer.android.com/topic/libraries/data-binding/index.html

I can not understand what not so do.

android { .... dataBinding { enabled = true } }

<data>
    <variable
        name="presenter"
        type="ua.com.it_st.ordersmanagers.activiteies.HistoryActivity"/>
</data>

<Button
    android:id="@+id/test"
    android:text="Start second activity"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:clickable="true"
    android:onClick="@{presenter::onHistoryClick}"
    />

public class HistoryActivity extends MvpAppCompatActivity implements HistoryView {

    @InjectPresenter
    HistoryPresenter historyPresenter;

    ActivityHistoryBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
         binding = DataBindingUtil.setContentView(this, R.layout.activity_history);
    }

    public void onHistoryClick(View view) {
        Log.i("test ","test");
        binding.test.setText("test");
    }
like image 436
Gennadii Ianchev Avatar asked Jul 24 '17 19:07

Gennadii Ianchev


1 Answers

You forgot to set the ViewModel to your binding. If you expect the onClick to be received in your activity, you have to do

binding.setPresenter(this)

although I would recommend calling the ViewModel in your XML file viewModel or activity (it's called presenter right now). If you want your presenter ot receive the onClick, change the ViewModel type in your layout from activity to presenter, implement the onClick method in your presenter, and do

binding.setPresenter(presenter)
like image 97
fweigl Avatar answered Sep 20 '22 19:09

fweigl