我使用Azure移动服务来支持我的Android应用,在这种情况下,我尝试使用此方法检索某些数据库数据:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
final MobileServiceList<ActivityTable> result = activitiesTable.execute().get();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
if (!(result.isEmpty())) {
for (ActivityTable item : result) {
final ActivityTable activity = new ActivityTable();
activity.setID(item.getId());
activity.setContent(item.getContent());
activity.setUserID(item.getUserID());
activity.setMedia(item.getMedia());
activity.setType((item.getType()));
UserTable user = GetUser(item.getUserID());
activity.setUser(user);
activities.add(activity);
}
//Populate ListView
PopulateNewsreel();
} else {
Log.e("test", "Error in Activity Loading 1");
}
}
});
} catch (Exception exception) {
Log.e("test","Error in Activity Loading 2");
Log.e("test",exception.toString());
}
return null;
}
}.execute();
然后我使用检索到的数据填充我的列表视图:
ListView newsreel = (ListView) getActivity().findViewById(R.id.newsreel_list);
NewsfeedAdapter adapter = new NewsfeedAdapter(getActivity(),R.layout.activity_newsfeed,activities);
newsreel.setAdapter(adapter);
但是,列表视图填充会延迟,有时列表视图中不会显示任何数据,但需要时间。在大约3秒的延迟之后,填充数据,直到那时listview用它们的占位符填充listview项。你明白了。
如何在显示视图之前等待检索完成,以便我可以填充列表视图?