我一直在一个小项目中,从数据库加载数据并通过api Web服务在不同的列表视图中显示它们。
此刻,我正在使用以下方法。 为了填充一个列表,我进行了一次改造,然后将其传递给适配器以显示数据。然后进行下一个调用以填充另一个列表,然后填充另一个列表,直到所有列表被填充。
还有更好的方法吗?
为了说明我的问题,我将使用虚拟功能代码。
我发送请求的Fragment类是:
public class FragmentDummy extends Fragment {
final private String myURL = "myURL";
private ListView listOne;
private ListView listTwo;
private ListView listThree;
private void initViews(View view) {
listOne = view.findViewById(R.id.lv_first);
listTwo = view.findViewById(R.id.lv_second);
listThree = view.findViewById(R.id.lv_third);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.test_dummy_layout, container, false);
initViews(view);
return view;
}
@Override
public void onActivityCreated(Bundle state) {
super.onActivityCreated(state);
FillListOne();
}
private List<ManagerList> listResponse = new ArrayList<>();
private void FillListOne() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(myURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceList listInfo = retrofit.create(ServiceList.class);
Call<List<ManagerList>> listRequest = listInfo.getListOne();
listRequest.enqueue(new Callback<List<ManagerList>>() {
@Override
public void onResponse(Call<List<ManagerList>> call,
Response<List<ManagerList>> response) {
listResponse = response.body();
AdapterList adapterListOne = new AdapterList(getContext(),
listResponse);
listOne.setAdapter(adapterListOne);
}
@Override
public void onFailure(Call<List<ManagerList>> call, Throwable t) {
}
});
}
private void FillListTwo() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(myURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceList listInfo = retrofit.create(ServiceList.class);
Call<List<ManagerList>> listRequest = listInfo.getListTwo();
listRequest.enqueue(new Callback<List<ManagerList>>() {
@Override
public void onResponse(Call<List<ManagerList>> call,
Response<List<ManagerList>> response) {
listResponse = response.body();
AdapterList adapterListTwo = new AdapterList(getContext(),
listResponse);
listTwo.setAdapter(adapterListTwo);
FillListThree();
}
@Override
public void onFailure(Call<List<ManagerList>> call, Throwable t) {
}
});
}
private void FillListThree() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(myURL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServiceList listInfo = retrofit.create(ServiceList.class);
Call<List<ManagerList>> listRequest = listInfo.getListThree();
listRequest.enqueue(new Callback<List<ManagerList>>() {
@Override
public void onResponse(Call<List<ManagerList>> call, Response<List<ManagerList>> response) {
listResponse = response.body();
AdapterList adapterListThree = new AdapterList(getContext(), listResponse);
listThree.setAdapter(adapterListThree);
}
@Override
public void onFailure(Call<List<ManagerList>> call, Throwable t) {
}
});
}
}
我的界面类:
public interface ServiceList {
@GET("api/listOne")
Call<List<ManagerList>> getListOne();
@GET("api/listTwo")
Call<List<ManagerList>> getListTwo();
@GET("api/listTree")
Call<List<ManagerList>> getListThree();
}
ManagerList类是我定义对象结构的地方。
AdapterList类是填充ListView的类。
我的xml布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_gravity="center"
tools:context=".FragmentDummy">
<TextView
android:id="@+id/tv_firstTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:text="First List"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/steelblue"
android:textSize="@dimen/title_size"/>
<ListView
android:id="@+id/lv_first"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"
android:textSize="@dimen/info_size" />
<TextView
android:id="@+id/tv_secondTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:text="Second List"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/steelblue"
android:textSize="@dimen/title_size"/>
<ListView
android:id="@+id/lv_second"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"
android:textSize="@dimen/info_size" />
<TextView
android:id="@+id/tv_thirdTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:text="Third List"
android:textStyle="bold"
android:textAlignment="center"
android:textColor="@color/white"
android:background="@color/steelblue"
android:textSize="@dimen/title_size"/>
<ListView
android:id="@+id/lv_third"
android:layout_width="match_parent"
android:layout_height="100dp"
android:textAlignment="center"
android:layout_marginBottom="10dp"
android:textSize="@dimen/info_size" />
</LinearLayout>
我已经阅读到可以使用Rx Java对其进行改进和优化,但是主题非常多,我不知道从哪里开始改进我的代码。
我知道我可以使用ReciclerView代替ListView,但这只是为了举例说明服务电话。