0
我想從AsyncTask類中的API中獲取數據並將數據返回到數據在RecyclerView中顯示的片段。但數據總是空的,片段總是空的。asynctask類沒有返回數據片段
看看我的代碼。
我的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);
}
可有人點我要去的地方錯了嗎?我會很感激幫助。
謝謝
謝謝。它工作完美。你能告訴我們如何將這些數據作爲緩存數據存儲在API中,以便我們即使在離線時也能訪問它? – vinitpradhan18
簡短回答:SQLite,Realm數據庫或Firebase。儘管如此,請隨意發佈一個單獨的問題 –