我正在使用MVVM模式通过数据绑定在回收器视图中从服务器获取数据。我正在执行存储库类中所有基于服务器的操作。
这是我到目前为止所做的:
MainAcivity.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>
Employee.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;
}
}
Repository.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() {
}
};
}
}
这里我如何在回收器视图中的存储库类中显示从表单服务器获取的数据列表。请让我知道我做错了或缺少任何帮助。
谢谢