java - 如何使用数据绑定在回收站视图中从服务器获取数据
问题描述
我正在使用 MVVM 模式使用数据绑定从回收站视图中的服务器获取数据。我在存储库类中执行所有基于服务器的操作。
这是我到目前为止所做的:
主活动.java
ActivityMainBinding activityMainBinding;
Repository repo;
EmployAdapter adapter;
List<Employee> empList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activityMainBinding = DataBindingUtil.setContentView(this,R.layout.activity_main);
setSupportActionBar(activityMainBinding.toolbar);
repo = new Repository(this);
activityMainBinding.content.recyclerView.setHasFixedSize(true);
activityMainBinding.content.recyclerView.setLayoutManager(new LinearLayoutManager(this));
}
EmployeeAdapter.java
public class EmployAdapter extends RecyclerView.Adapter<EmployAdapter.ViewHolder> {
Context context;
List<Employee> empList = new ArrayList<>();
public EmployAdapter(Context context, List<Employee> empList) {
this.context = context;
this.empList = empList;
}
@NonNull
@Override
public EmployAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RowLayoutBinding rowLayoutBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.row_layout,parent,false);
ViewHolder viewHolder = new ViewHolder(rowLayoutBinding);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull EmployAdapter.ViewHolder holder, int position) {
Employee model = empList.get(position);
holder.rowLayoutBinding.setEmplo(model);
}
@Override
public int getItemCount() {
return empList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
RowLayoutBinding rowLayoutBinding;
public ViewHolder(@NonNull RowLayoutBinding itemView) {
super(itemView.getRoot());
rowLayoutBinding = itemView;
}
}
}
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<layout>
<data>
<variable
name="emplo"
type="Models.Employee"/>
</data>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{emplo.name}"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{emplo.age}"
android:textColor="@android:color/darker_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</layout>
雇员.java
public class Employee {
@SerializedName("Name")
@Expose
String Name;
@SerializedName("Age")
@Expose
String Age;
public Employee(){
}
public Employee(String Name,String Age){
this.Name = Name;
this.Age = Age;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getAge() {
return Age;
}
public void setAge(String age) {
Age = age;
}
}
存储库.java
public class Repository {
Context context;
public Repository(Context context){
this.context = context;
}
public void getEmployee(){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService apiService = retrofit.create(ApiService.class);
apiService.getDataFromServer().subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(getDataObserver());
}
private Observer<List<Employee>> getDataObserver(){
return new Observer<List<Employee>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Employee> employees) {
List<Employee> list = employees;
for(int i = 0;i<list.size();i++){
String name = list.get(i).getName();
String age = list.get(i).getAge();
Employee empModel = new Employee(name,age);
}
}
@Override
public void onError(Throwable e) {
Toast.makeText(context,"Error: "+e.getMessage(),Toast.LENGTH_LONG).show();
}
@Override
public void onComplete() {
}
};
}
}
在这里,我如何在回收站视图中显示存储库类中从服务器获取的数据列表。有人请让我知道我做错了什么或缺少任何帮助将不胜感激。
谢谢
解决方案
推荐阅读
- spring-boot - 通过服务调用时文件上传失败,即restTemplate.postForEntity
- jmeter - PerfMon 指标收集器图显示最多 4 分钟的信息,但脚本执行的实际时间为 10 分钟
- python - 如何在ORACLE表中插入json数据?
- sql - 循环所有表并执行存储过程
- javascript - PHP将文件发送到Web服务器无法访问的浏览器
- jquery - 当您在每个循环中修改 jQuery 集合时,是包含修改后的集合还是仅包含原始集合?
- vue.js - 无法在 vuetify 项目中添加自定义颜色
- git - Git rebase to orphan 导致二进制文件冲突
- c# - 可空值类型上的提升运算符是否使用短路?
- php - 在每个函数调用中重新加载使用“require”检索的 php 文件