在我的应用程序片段中,我arrayList
通过在方法中为服务器加载数据来设置值onCreate()
,然后我想在listView
适配器的帮助下将该数据显示到其中,但是当我运行我的程序时,它onCreateView()
首先在onCreate()
方法中的网络代码之前执行
onCreate()
ArrayList<ContactItem> contactsList = new ArrayList<ContactItem>();
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final QBUser qbUser = new QBUser("username", "password");
QBAuth.createSession(qbUser).performAsync(new QBEntityCallback<QBSession>() {
@Override
public void onSuccess(QBSession qbSession, Bundle bundle) {
Toast.makeText(getActivity(), "created", Toast.LENGTH_SHORT).show();
for (int i = 0; i < contactName.size(); i++) {
QBUsers.getUserByLogin(contactNumber.get(i).replaceAll(" ", "")).performAsync(new QBEntityCallback<QBUser>() {
@Override
public void onSuccess(QBUser qbUser, Bundle bundle) {
if (qbUser != null) {
final ContactItem contactItem = new ContactItem(qbUser.getFullName(), qbUser.getLogin(), R.drawable.profile);
contactsList.add(contactItem);
}
}
@Override
public void onError(QBResponseException e) {
}
});
}
}
@Override
public void onError(QBResponseException e) {
Toast.makeText(getActivity(), "failed " + e.toString(), Toast.LENGTH_LONG).show();
}
});
}
onCreateView()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_contacts, container, false);
ListView listView = (ListView) rootView.findViewById(R.id.contacts_listview);
contactItemAdapter = new ContactItemAdapter(getActivity(), contactsList);
listView.setAdapter(contactItemAdapter);
return rootView;
}