Android数据绑定无法正常工作 [英] The android data binding is not working properly

查看:183
本文介绍了Android数据绑定无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想帮助解决问题。

首先,按照我的代码的细节:

First, following the details of my code:

build.gradle (Project: android)

buildscript {
    repositories {
        jcenter()
        mavenCentral()
        maven { url '/home/melti/java/repository' }
    }
    dependencies {
        classpath "com.android.tools.build:gradle:1.3.0-beta4"
        classpath "com.android.databinding:dataBinder:1.0-rc0"

    }
}

allprojects {
    repositories {
        jcenter()
        mavenCentral()
        maven { url '/home/melti/java/repository' }

    }
}

build.gradle(模块:应用程序)

build.gradle (Module: app)

apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'

android {
    compileSdkVersion 22
    buildToolsVersion "23.0.0 rc2"

    defaultConfig {
        applicationId "br.com.soma"
        minSdkVersion 16
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
    compile 'com.android.support:recyclerview-v7:21.0.3'
    compile 'com.android.support:cardview-v7:21.0.3'
    compile 'com.squareup.picasso:picasso:2.5.2'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.google.code.gson:gson:2.3.1'
    compile 'org.springframework:spring-core:4.1.7.RELEASE'
    compile 'org.apache.commons:commons-io:1.3.2'


}

AmanteEditModel

AmanteEditModel

package br.com.soma.amante.edit;

import android.databinding.BaseObservable;
import android.databinding.Bindable;

import br.com.soma.BR;

/**
 * Created by spassu on 09/07/15.
 */
public class AmanteEditModel extends BaseObservable {

    private String senhaConfirm;



    @Bindable
    public String getSenhaConfirm() {
        return senhaConfirm;
    }

    public void setSenhaConfirm(String senhaConfirm) {
        this.senhaConfirm = senhaConfirm;
        notifyPropertyChanged(BR.senhaConfirm);
    }
}

fragment_amante_edit

fragment_amante_edit

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto">

    <data>
        <variable name="model" type="br.com.soma.amante.edit.AmanteEditModel"/>
    </data>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <android.support.v7.widget.CardView
            xmlns:android="http://schemas.android.com/apk/res/android"
            style="@style/card_edit_style"
            android:layout_gravity="top"
            card_view:cardCornerRadius="0dp">


                    <EditText
                        android:id="@+id/amante_edit_senha_confirm"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:hint="Confirme a senha"
                        android:inputType="textPassword"
                        android:maxLines="1"
                        android:text="@{model.senhaConfirm}"
                    />

        </android.support.v7.widget.CardView>

    </ScrollView>
</layout>

AmanteEditFragment

AmanteEditFragment

package br.com.soma.amante.edit;

import android.app.Fragment;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import br.com.soma.R;
import br.com.soma.amante.view.AmanteViewActivity;
import br.com.soma.databinding.FragmentAmanteEditBinding;

/**
 * Created by spassu on 27/05/15.
 */
public class AmanteEditFragment extends Fragment {

    private AmanteEditModel model;

    private static final String AMANTE_ID = "amanteId";
    public static final String DEFAULT_FRAGMENT_TAG = "amanteEditFragment";

    // Views
    private long amanteId;

    public AmanteEditFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        model = new AmanteEditModel();
        FragmentAmanteEditBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_amante_edit, container, false);
        binding.setModel(model);
        return binding.getRoot();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.menu_amante_edit, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()) {
            // The Save button was pressed
            case R.id.menu_amante_edit_save:
                save();
                getActivity().finish();
                return true;
            }
        return super.onOptionsItemSelected(item);
    }


    public static AmanteEditFragment newInstance(long id) {
        AmanteEditFragment fragment = new AmanteEditFragment();
        Bundle args = new Bundle();
        args.putLong(AmanteViewActivity.EXTRA_AMANTEID, id);
        fragment.setArguments(args);
        return fragment;
    }

    private void save() {
        Toast.makeText(getActivity(), model.getSenhaConfirm(), Toast.LENGTH_SHORT).show();
    }
}

senhaConfirm中输入数据后并点击 save(),我发现绑定不起作用,也就是 senhaConfirm AmanteEditModel 是空的。任何人可以帮助我吗?

After enter data in senhaConfirm and clickingsave(), I found that the binding does not work, that is, the senhaConfirm into AmanteEditModel is null yet. Can anyone help me?

推荐答案

编辑:根据Remi David Android Studio现在具有双向数据绑定功能。我下面描述的解决方案不应该是不必要的。

EDIT: According to Remi David Android Studio now features two way databinding. The solution I describe below should no longer be nessesary.

谢谢Remi指出来。

对于用户输入,Android Databinding只是一种方式。 GUI将自动反映模型中的任何更改,但反之亦然。

With respect to user input Android Databinding is only one way. The GUI will automagically reflect any changes in the model, but not vice versa.

您需要在模型类中添加一个TextChangeListener,该模型类在用户更改时设置模型属性任何事情

You need to add a TextChangeListener to your model class which sets the model property when the user changes anything.

示例:

public class AmanteEditModel extends BaseObservable {

    private String senhaConfirm;

    @Bindable
    public String getSenhaConfirm() {
        return senhaConfirm;
    }

    public void setSenhaConfirm(String senhaConfirm) {
        this.senhaConfirm = senhaConfirm;
        notifyPropertyChanged(BR.senhaConfirm);
    }

    // Textwatcher Reference: http://developer.android.com/reference/android/text/TextWatcher.html
    public TextWatcher getMyEditTextWatcher() {
        return new TextWatcher() {

            public void afterTextChanged(Editable s) {
            }

            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {
            }

            public void onTextChanged(CharSequence s, int start,
                                  int before, int count) {
                // Important! Use the property setter, otherwhise the model won't be informed about the change.
                setSenhaConfirm(s);
            }
        };
    }

}

在您的布局中,xml将EditText更改为这个:

In your layout xml change EditText to this:

<EditText
    android:id="@+id/amante_edit_senha_confirm"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:hint="Confirme a senha"
    android:inputType="textPassword"
    android:maxLines="1"
    android:text="@{model.senhaConfirm}"
    app:addTextChangeListener="@{model.myEditTextWatcher}"
    />

查看 addTextChangeListener 的命名空间。此方法可能无法通过android:命名空间,所以我使用的应用程序:这里。您也可以使用bind:使绑定更加清晰。

Watch for the namespace of addTextChangeListener. This method might not be available through the android: namespace, so I'm using app: here. You may also use bind: to make the binding more clear.

所以不要错过添加

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:bind="http://schemas.android.com/apk/res-auto"

到您的XML命名空间。

to your XML namespaces.

此解决方案适用于所有输入控件,自定义由于您在模型中提供了正确的侦听器。

This solution works for all input controls, custom included, given you provide the correct Listeners in your model.

TextWatcher参考

这篇关于Android数据绑定无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆