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

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

问题描述

我想帮助解决问题。

首先,在我的code中的细节:

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 ,点击保存(),我发现,绑定不工作,那输入数据后,是时, 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?

推荐答案

对于用户输入的Andr​​oid数据绑定只有一个办法。图形用户界面将自动地反映在模型中的任何变化,而不是相反。

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这个方法可能无法使用:命名空间,所以我使用的应用程序:在这里。你也可以使用绑定:使结合更加清晰。

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.

所以,千万不要错过添加

So don't miss to add

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天全站免登陆