android - 是否可以使用返回字符串的 ViewModel 方法设置 TextView 的文本?
问题描述
是否可以使用返回字符串的方法更改 TextView 的文本?
出于培训目的,我创建了这个ViewModel
类,它将User
对象放入List<User> mUserList
其中,我想知道我是否可以使用 ViewMode 方法将此用户的名字设置为 TextView 的文本。我收到绑定错误,是否可以使用不使用 @BindingAdapter 注释返回 String 的方法来解决此问题?
编辑:当我设置android:text="@{viewmodel.userList.toString()}"
它不返回绑定错误但它返回用户对象的字符串表示。但是当我使用android:text="@{viewmodel.getUserFirstNames()}"
我得到一个绑定错误。为什么会这样?
public class UsersViewModel extends ViewModel {
private List<User> mUserList;
public List<User> getUserList() {
if (mUserList == null) {
mUserList = loadUsers();
}
return mUserList;
}
/**
* Dummy Method to fake web service
*
* @return list of users
*/
private List<User> loadUsers() {
// do something to load users
List<User> userList = new ArrayList<>();
User user = new User();
user.setFirstName("John");
user.setLastName("Adams");
userList.add(user);
user = new User();
user.setFirstName("Lucy");
user.setLastName("Adams");
userList.add(user);
return userList;
}
@NonNull
private String getUserFirstNames() {
if (mUserList != null && mUserList.size() > 0) {
StringBuilder sb = new StringBuilder();
sb.append("First Names:\n");
for (User user : mUserList) {
sb.append(user.getFirstName() + "\n");
}
return sb.toString();
}
return "empty";
}
}
在 MainActivity 我假装使用公共类 MainActivity 加载数据扩展 AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
UsersViewModel usersViewModel =
ViewModelProviders.of(this).get(UsersViewModel.class);
usersViewModel.getUserList();
activityMainBinding.setViewmodel(usersViewModel);
}
}
在布局中我想知道是否可以使用 ViewModel 类设置 TextView 的文本
<data>
<variable
name="viewmodel"
type="com.example.tutorial1basics.viewmodel.UsersViewModel" />
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/tv_users"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{viewmodel.getUserFirstNames()}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
</layout>
解决方案
您想使用 BaseObservable 扩展您的模型。
喜欢 :
public class Post extends BaseObservable{
@SerializedName("userId")
@Expose
private Integer userId;
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("title")
@Expose
private String title;
@SerializedName("body")
@Expose
private String body;
@Bindable
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
notifyPropertyChanged(BR.userId);
}
@Bindable
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
notifyPropertyChanged(BR.id);
}
@Bindable
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
notifyPropertyChanged(BR.title);
}
@Bindable
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
notifyPropertyChanged(BR.body);
}
}
推荐阅读
- android - 如何在 ScrollView + 相对布局中使 TextView 靠近 EditText
- ffmpeg - 如何使用ffmpeg记录不连续的rtsp流?
- spring - 在 kotlin 中访问 Spring 托管类之外的 Spring 托管 bean
- java - 如何找到每行代码的执行时间?
- python - 为什么while循环无法中断?
- javascript - TypeError:每次我重新加载网页时都无法读取未定义的属性“1”
- c++ - 重载 C 字符串的函数模板
- javascript - Angular 8 RangeError:超出最大调用堆栈大小
- c++ - C++ 如何修改代码以在仅编译范围为 0x00 到 0x7F 的代码中从 0x00 到 0xFF 范围内的串行端口发送字节
- python - 如何从 PyQT5 创建可安装 (.exe) 文件