【问题标题】:Android AsyncTask wait until finished to retrieve data and display it in the ListViewAndroid AsyncTask 等待完成以检索数据并将其显示在 ListView
【发布时间】:2021-05-20 05:02:45
【问题描述】:

我有这个方法

private void changeContacts() {
        if (mOnlyDisplayContacts) {
                listContact = fetchContactResponse(mView);
            } else {
                // Other code
            }

        contactAdapter = new ContactsAdapter(context, listContact, this);

        mContactsList.setAdapter(mContactAdapter);

        mContactAdapter.notifyDataSetChanged();

    }

private List<Contact> fetchContactResponse(final String view) {


        AsyncContactSearch mLoadContactTask = new AsyncContactSearch(context, limit, offset, view, search);
        try {
            listContacts = mLoadContactTask.execute().get();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return listContacts;
    }

课堂任务

public class AsyncContactSearch extends AsyncTask<Void, Void, List<LinphoneContact>> {

    private Context context;
    private int limit, offset;
    private String view, search;

    public AsyncContactSearch(Context context, int limit, int offset, String view, String search) {
        this.context = context;
        this.limit = limit;
        this.offset = offset;
        this.view = view;
        this.search = search;
    }

    @Override
    protected List<Contact> doInBackground(Void... voids) {
        String domain = SharedPreferencesManager.getDomain(context);
        String authToken = SharedPreferencesManager.getAuthtoken(context);
        final List<Contact> listContact = new ArrayList<>();

        RestAPI RestAPI = RetrofitHelper.create(RestAPI.class, domain);

        Call<ContactList> searchWithTerms =
                userRestAPI.searchWithTerms(authToken, "", limit, offset);
        searchWithTerms.enqueue(
                new Callback<ContactList>() {
                    @Override
                    public void onResponse(Call<ContactList> call, Response<ContactList> response) {
                        ContactList contactList = response.body();
                        if (contactList == null) {
                            return;
                        }
                        List<Contact> contacts = contactList.getRows();
                        for (Contact c : contacts) {
                            listContact.add(
                                    ContactsManager.getInstance().addFromAPI(c));
                        }
                    }

                    @Override
                    public void onFailure(Call<ContactList> call, Throwable throwable) {}
                });

        Collections.sort(
                listContact,
                new Comparator() {

                    public int compare(Object o1, Object o2) {

                        String x1 = ((LinphoneContact) o1).getCompany();
                        String x2 = ((LinphoneContact) o2).getCompany();
                        int sComp = x1.compareTo(x2);

                        if (sComp != 0) {
                            return sComp;
                        }

                        String x3 = ((LinphoneContact) o1).getFirstName();
                        String x4 = ((LinphoneContact) o2).getFirstName();
                        return x3.compareTo(x4);
                    }
                });
        return listContact;
    }
}

问题是(调试代码)当搜索任务还在运行时,立即触发方法contactAdapter = new ContactsAdapter(context, listContact, this);listContact为空,然后执行继续将Adapter分配给ListView,而恢复任务继续并将元素插入到 List 中,在屏幕上 ListView 保持为空

【问题讨论】:

    标签: android android-asynctask


    【解决方案1】:

    您正在使用 API 调用的改造,因此无需使用 AsyncTask。改造将使 API 调用 asynchronously 并在 callback onResponse() 中传递结果。只需将您的逻辑移入callback onResponse()

    【讨论】:

      猜你喜欢
      • 2020-12-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多