首页 > 解决方案 > 如何使用数据绑定在回收站视图中从服务器获取数据

问题描述

我正在使用 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() {

        }
     };
   }
 } 

在这里,我如何在回收站视图中显示存储库类中从服务器获取的数据列表。有人请让我知道我做错了什么或缺少任何帮助将不胜感激。

谢谢

标签: javaandroidretrofit2rx-java2

解决方案


推荐阅读