我正在尝试从AsyncTask类中的API获取数据,并将数据返回到数据显示在RecyclerView中的片段。但是数据始终为空,并且片段始终为空。
看看我的代码。
我的AsyncTask类:
public class EmployeesData extends AsyncTask<Void, Void, List<Employee>>{
List<Employee> data = new ArrayList<>();
EmployeesFragment emp;
public EmployeesData (EmployeesFragment emp){
this.emp= emp;
}
@Override
protected List<Employee> doInBackground(Void... arg0) {
RestService restService = new RestService();
try {
restService.getService().getEmployees(new Callback<List<Employee>>() {
@Override
public void success(List<Employee> employee, Response response) {
data = employee;
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(emp.getContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
} catch (Exception ex) {
Toast.makeText(emp.getContext(), ex.getMessage().toString(), Toast.LENGTH_LONG).show();
}
return data;
}
@Override
protected void onPostExecute(List<Employee> data) {
super.onPostExecute(data);
emp.setList(data);
}
}
我的片段:
public class EmployeesFragment extends Fragment {
RecyclerView rv;
List<Employee> employees= new ArrayList<>();
public EmployeesFragment () {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_employees, container, false);
rv = (RecyclerView) view.findViewById(R.id.emp_Rv);
rv.setLayoutManager(new LinearLayoutManager(getContext()));
fireYourAsyncTask();
EmployeesRvAdapter adapter = new EmployeesRvAdapter (employees, getActivity());
rv.setAdapter(adapter);
return view;
}
public void setList(List<Employee> data) {
employees= data;
}
public void fireYourAsyncTask() {
new EmployeesData(this).execute();
}
}
我的RecyclerView适配器:
public class EmployeesRvAdapter extends RecyclerView.Adapter<EmployeesRvAdapter.EmployeesHolder> {
List<Employees> employees;
LayoutInflater inflater;
Context context;
public class EmployeesHolder extends RecyclerView.ViewHolder {
TextView name, description;
View container;
public EmployeesHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.name_tv);
description = (TextView) itemView.findViewById(R.id.description_tv);
container = itemView.findViewById(R.id. item_root);
}
}
public EmployeesRvAdapter(List<Employees> employees, Context c) {
this.inflater = LayoutInflater.from(c);
this.employees = employees;
context = c;
}
@Override
public EmployeesRvAdapter.EmployeesHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = inflater.inflate(R.layout.employees_rv_item, parent, false);
return new EmployeesRvAdapter.EmployeesHolder(v);
}
@Override
public void onBindViewHolder(final EmployeesRvAdapter.EmployeesHolder holder, int position) {
Employees employee = Employees.get(position);
holder.name.setText(employee.getName());
holder.description.setText(employee.getDescription());
}
@Override
public int getItemCount() {
return employees.size();
}
}
我的RestService类:
public class RestService {
private static final String URL = "http://localhost:23000/";
private retrofit.RestAdapter restAdapter;
private EmployeesService apiService;
public RestService()
{
restAdapter = new retrofit.RestAdapter.Builder()
.setEndpoint(URL)
.setLogLevel(retrofit.RestAdapter.LogLevel.FULL)
.build();
apiService = restAdapter.create(EmployeesService.class);
}
public EmployeesService getService()
{
return apiService;
}
}
我的员工服务界面:
public interface EmployeesService {
@GET("/api/employees")
public void getEmployees(Callback<List<Employee>> callback);
}
有人指出我错了吗?我很感激帮助。
谢谢
答案 0 :(得分:1)
首先,您不需要使用Retrofit的AsyncTask。
可能只是这个
private EmployeesRvAdapter adapter;
public void fireYourAsyncTask() {
EmployeesService service = new RestService().getService();
try {
service.getEmployees(new Callback<List<Employee>>() {
@Override
public void success(List<Employee> employee, Response response) {
employees.clear();
employees.addAll(employee);
adapter.notifyDatSetChanged();
}
@Override
public void failure(RetrofitError error) {
Toast.makeText(emp.getContext(), error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
});
如果你这样做会更清洁,但这取决于你
public class EmployeesFragment extends Fragment implements Callback<List<Employee>> {
而且更清洁,我的意思是它只是
public void fireYourAsyncTask() {
EmployeesService service = new RestService().getService();
service.getEmployees(this);
}
无论您在何处添加数据,这都不是您添加到ArrayList的方式
employees= data;
这是重新分配。这是补充
employees.clear();
employees.addAll(data);