我正在开发一个Android应用程序,它有PullToRefreshListView。但我无法将自定义列表项加载到该列表视图中。
listview数据是从AsyncTask中的webservice调用加载的。但是当我从onPostExecute()设置适配器时,即使调用适配器的getView(),它也不会显示任何项目。
列表视图位于片段中:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_myproject, container,
false);
context = getActivity();
projectsPullToRefreshView = (PullToRefreshListView) view
.findViewById(R.id.projects_listview);
projectsListView = projectsPullToRefreshView.getRefreshableView();
projectsPullToRefreshView.setOnItemClickListener(this);
myProjects = new ArrayList<Project>();
projectsAdapter = new ProjectsListAdapter(context,
R.layout.activity_projects_listitem, myProjects, this);
projectsPullToRefreshView.setAdapter(projectsAdapter);
// Set a listener to be invoked when the list should be refreshed.
projectsPullToRefreshView
.setOnRefreshListener(new OnRefreshListener<ListView>() {
@Override
public void onRefresh(
PullToRefreshBase<ListView> refreshView) {
String label = DateUtils.formatDateTime(
getActivity(),
System.currentTimeMillis(),
DateUtils.FORMAT_SHOW_TIME
| DateUtils.FORMAT_SHOW_DATE
| DateUtils.FORMAT_ABBREV_ALL);
// Update the LastUpdatedLabel
refreshView.getLoadingLayoutProxy()
.setLastUpdatedLabel(label);
// Do work to refresh the list here.
loadAllProjects();
}
});
// Add an end-of-list listener
projectsPullToRefreshView
.setOnLastItemVisibleListener(new OnLastItemVisibleListener() {
@Override
public void onLastItemVisible() {
Toast.makeText(getActivity(), "End of List!",
Toast.LENGTH_SHORT).show();
}
});
loadAllProjects();
projectsPullToRefreshView.setRefreshing(false);
return view;
}
并且AsyncTask有一个自定义回调,
@Override
public void onWebServiceCompleted(Object response, int requestCode) {
// TODO Auto-generated method stub
if (requestCode == REQUEST_GET_PROJECTS) {
if (project.getProjects() != null) {
myProjects.addAll(project.getProjects());
projectsAdapter.notifyDataSetChanged();
projectsPullToRefreshView.onRefreshComplete();
}
}
}
当我在普通的ListView中使用相同的适配器时,它可以正常工作。