Android AsyncTask 等待完成以检索数据并将其显示在 ListView

Posted

技术标签:

【中文标题】Android AsyncTask 等待完成以检索数据并将其显示在 ListView【英文标题】:Android AsyncTask wait until finished to retrieve data and display it in the 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 保持为空

【问题讨论】:

【参考方案1】:

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

【讨论】:

以上是关于Android AsyncTask 等待完成以检索数据并将其显示在 ListView的主要内容,如果未能解决你的问题,请参考以下文章

等待 AsyncTask 完成,然后在 Android Java 中执行下一个代码

在 AsyncTask 完成后做某事

等待 onPostExecute 完成以制作新适配器

等待多个AsyncTask完成

让一个 AsyncTask 等待另一个完成?

在返回结果之前等待 AsyncTask.execute(()) 完成