简体   繁体   English

android数据绑定无法正常工作

[英]The android data binding is not working properly

I would like to help to fix a problem. 我想帮助解决问题。

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 (Module: app) build.gradle(模块:应用程序)

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 阿曼特编辑模型

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 阿曼特编辑片段

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();
    }
}

After enter data in senhaConfirm and clicking save( ), I found that the binding does not work, that is, the senhaConfirm into AmanteEditModel is null yet. 之后进入数据senhaConfirm并单击save( ),我发现,绑定不工作,也就是senhaConfirmAmanteEditModel为空呢。 Can anyone help me? 谁能帮我?

EDIT : According to Remi David Android Studio now features two way databinding. 编辑 :根据雷米·大卫的说法,Android Studio现在具有两种方式的数据绑定。 The solution I describe below should no longer be nessesary. 我下面描述的解决方案不再是必需的。

Thanks Remi for pointing that out. 感谢Remi指出这一点。


With respect to user input Android Databinding is only one way. 关于用户输入,Android数据绑定只是一种方法。 The GUI will automagically reflect any changes in the model, but not vice versa. GUI将自动反映模型中的任何更改,反之亦然。

You need to add a TextChangeListener to your model class which sets the model property when the user changes anything. 您需要向模型类添加一个TextChangeListener,当用户进行任何更改时,该类将设置模型属性。

Example: 例:

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);
            }
        };
    }

}

In your layout xml change EditText to this: 在布局xml中,将EditText更改为:

<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}"
    />

Watch for the namespace of addTextChangeListener . 监视addTextChangeListener的名称空间。 This method might not be available through the android: namespace, so I'm using app: here. 此方法可能无法通过android:命名空间使用,因此我在这里使用app:。 You may also use bind: to make the binding more clear. 您也可以使用bind:使绑定更清晰。

So don't miss to add 所以不要错过添加

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

or 要么

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

to your XML namespaces. 您的XML名称空间。

This solution works for all input controls, custom included, given you provide the correct Listeners in your model. 如果您在模型中提供了正确的侦听器,则此解决方案适用于所有输入控件(包括自定义)。

TextWatcher Reference TextWatcher参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM