2017-01-08 1 views
0

예전에 this one처럼 몇 가지 논의가있었습니다. 그러나 그것은 나를 위해 일하는 것 같지 않습니다. 여기 내 레이아웃에있는 내용은 다음과 같습니다.Android에서 양방향 데이터 바인딩이 작동하지 않음

<TextView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@{word.contents}" 
     android:id="@+id/wordView"/> 
    <EditText android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/wordInput" 
     android:text="@={word.contents}"/> 

입력시 EditText에 업데이트 된 텍스트가 표시 될 것으로 예상합니다. 그러나 그것은 일어나지 않습니다. 내 자신의 Text Watchers를 구현하여 작동하도록 할 수는 있지만 데이터 바인딩의 모든 부분은 그렇게하지 않는 것이 좋다고 생각했습니다. 내가 여기서 아무것도 놓치고 있니? 더 많은 일을해야합니까? 데이터를 obeservable 또는 bindable하게 만들 것을 제안하는 일부 사람들을 찾았지만 차라리 내 모델 객체를 엉망으로 만들지는 않을 것입니다.

dataBinding { 
    enabled = true 
} 

을 내 MainActivity의에서 onCreate에서 :

classpath 'com.android.tools.build:gradle:2.2.3' 

내 응용 프로그램 Gradle을이 있습니다

MainBinding binding = DataBindingUtil.setContentView(this, R.layout.main); 
Word word = new Word("Test Word"); 
binding.setWord(word); 

을 여기에 말씀 클래스의 간단한 POJO :

여기 내 Gradle을 의존성이다
public class Word { 
    private String contents; 

    public Word() { 
     contents = ""; 
    } 

    public Word(String contents) { 
     this.contents = contents; 
    } 

    public String getContents() { 
     return contents; 
    } 

    public void setContents(String contents) { 
     this.contents = contents; 
    } 
} 

또한 디버깅 내 setContents 메서드에서 중단 점을 떠나는 응용 프로그램을 ged. EditText를 변경하면 코드가 중단 점에서 멈추고 모델이 실제로 변경된다는 것을 알 수 있습니다. TextView 구성 요소가 업데이트되지 않는 것처럼 보입니다. 어떤 생각?

+0

당신이 당신의'Word' 클래스를 공유 할 수 있습니까? – yigit

+0

Word 클래스 추가됨 – dilmali

답변

0

업데이트 자바 파일처럼

MainBinding binding = DataBindingUtil.setContentView(this, R.layout.main); 
Word word = new Word(); 
word.setContents("Test Word"); 
binding.setWord(word); 

당신 같은 말씀 클래스를 업데이트 할 수

public class Word extends BaseObservable { 
private String contents; 

public Word() { 
    contents = ""; 
} 

public Word(String contents) { 
    this.contents = contents; 
} 

@Bindable 
public String getContents() { 
    return contents; 
} 

public void setContents(String contents) { 
    this.contents = contents; 
    notifyPropertyChanged(BR.contents); 
    } 
} 
1

모델 클래스에 BaseObservableWord을 사용하고 해당 속성이 변경되었음을 알리면됩니다.

public class Word extends BaseObservable { 
    public void setContents(String contents) { 
     this.contents = contents; 
     notifyPropertyChanged(BR.contents); 
    } 
} 
관련 문제