使用数据绑定时,Android Studio不建议使用Xml MutableLiveData

时间:2018-05-27 17:38:47

标签: java android android-livedata mutablelivedata

当我们在XML中使用MutableLiveData时,我们在它中使用MutableLiveData时,Android Studio不提供给我们吗?

public class LoginViewModel extend ViewModel {

    public MutableLiveData<UserEntity> userEntity;

    public UserEntity userEntity1;

}

public UserEntity userEntity1;

enter image description here

userEntity1是Work,但userEntity不起作用。

如何解决此问题?

1 个答案:

答案 0 :(得分:1)

您需要在视图模型中使用公共getter来访问xml中的字段。

[WARN ] 2018-05-28 12:25:23.790 [http-nio-8000-exec-2] SqlExceptionHelper - SQL Error: 6550, SQLState: 65000
[ERROR] 2018-05-28 12:25:23.790 [http-nio-8000-exec-2] SqlExceptionHelper - ORA-06550: Ligne 1, colonne 7 :
PLS-00306: wrong number or types of arguments in call to 'STORED_P'
ORA-06550: Ligne 1, colonne 7 :
PL/SQL: Statement ignored

[ERROR] 2018-05-28 12:25:23.801 [http-nio-8000-exec-2] [dispatcherServlet] - Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: Error calling CallableStatement.getMoreResults] with root cause
java.sql.SQLException: ORA-06550: Ligne 1, colonne 7 :
PLS-00306: wrong number or types of arguments in call to 'STORED_P'
ORA-06550: Ligne 1, colonne 7 :
PL/SQL: Statement ignored

    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:450) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:399) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:1059) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]
    at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:522) ~[ojdbc7-12.1.0.2.jar:12.1.0.2.0]

然后你可以像这样访问xml中的字段:

选项1:

public class LoginViewModel extends ViewModel {

    private MutableLiveData<UserEntity> userEntity;

    //Mandatory zero parameter constructor, if non zero parameter constructor is necessary, a factory needs to be created for the ViewModel
    public LoginViewModel() {}

    //Option 1: Public getter for the userEntity object
    public MutableLiveData<UserEntity> getUserEntity() {
        return userEntity;
    }

    //Option 2: Alternatively a separate getter can be used for different fields of the model class
    public String getUserName() {
        return userEntity.getValue().getName();
    }
}

选项2:

android:text="@{userEntityViewModel.user.name}"

希望它有所帮助。