How to display contacts in Android using AsyncTask?

前端 未结 4 796
庸人自扰
庸人自扰 2021-01-07 14:43

I\'m trying to list all the contacts in mobile. So far everything was working fine, but when I thought of running my code in AsyncTask I\'m getting nothing on s

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-07 15:37

    You can't touch views in Non-UI thread. You must set adapter in onPostExecute();

    Use this: (Also, I hope that you keep your contacts somewhere, because I don't see contacts variable in your snippet)

    new AsyncTask() 
    {
    
        @Override
        protected void onPreExecute() 
        {
            pd = ProgressDialog.show(CallListActivity.this,
                    "Loading..", "Please Wait", true, false);
        }// End of onPreExecute method
    
        @Override
        protected Void doInBackground(Void... params) 
        {
            getContacts();
            return null;
        }// End of doInBackground method
    
        @Override
        protected void onPostExecute(Void result)
        {
            arrayAdapter = new ArrayAdapter(CallListActivity.this, R.layout.display_contacts,R.id.tv_single_contact_number,displayContactName);
            lvCallList.setAdapter(new CustomAdapter(CallListActivity.this));
            pd.dismiss();
    
        }//End of onPostExecute method
    }.execute((Void[]) null);
    

提交回复
热议问题