其他分享
首页 > 其他分享> > 如何使用AsyncTask在Android中显示联系人?

如何使用AsyncTask在Android中显示联系人?

作者:互联网

我正在尝试列出移动设备中的所有联系人.到目前为止一切正常,但是当我想在AsyncTask中运行我的代码时,屏幕上什么都没有.

这是代码片段

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_call_list);

    lvCallList = (ListView) findViewById(R.id.lv_call_list);

    new AsyncTask<Void, Void, Void>() 
    {
        @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)
        {
            pd.dismiss();

            lvCallList.setAdapter(new CustomAdapter(CallListActivity.this));

        }//End of onPostExecute method
    }.execute((Void[]) null);
     count = this.displayContactName.size();
    thumbnailsselection = new boolean[count];
}


private void getContacts()
{
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
    if (cur.getCount() > 0)
    {
        while (cur.moveToNext())
        {
            String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
            String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

            this.displayContactName.add(name);
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                        null,
                                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                        new String[]{id}, 
                                        null);
                while (pCur.moveToNext())
                {
                    String phoneNumber = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                    this.displayContactNumber.add(phoneNumber);
                } 
                pCur.close();
            }
        }
    }
}

 public class CustomAdapter extends BaseAdapter 
{
    /*
     * Variables Declaration section
     */
    private Context mContext;

    public CustomAdapter(Context context) 
    {
        mContext = context;
    }//End of CustomAdapter constructor

    public int getCount() 
    {
        return count;
    }//End of getCount method

    public Object getItem(int position) 
    {
        return position;
    }//End of getItem method

    public long getItemId(int position) 
    {
        return position;
    }//End of getItemId method

    public View getView(int position, View convertView, ViewGroup parent) 
    {

        return convertView;
    }//End of getView method
}//End of CustomAdapter instance inner class

class ViewHolder 
{
    TextView textviewName;
    TextView textviewNumber;
    CheckBox checkbox;
    int id;
}//End of ViewHolder instance inner class

使用pd.dismiss()处的断点显示displayContactName值;是

displayContactName  ArrayList  (id=830007844896)    
array   Object[12]  (id=830007907208)   
    [0] "Rrs" (id=830007907144) 
    [1] "Fgsd" (id=830007920424)    
    [2] null    
    [3] null    
    [4] null    
    [5] null    
    [6] null    
    [7] null    
    [8] null    
    [9] null    
    [10]    null    
    [11]    null    
modCount    2   
size    2   

我不知道我的代码有什么问题,当我不使用AsyncTask然后我的代码工作得很好,但是当我使用AsyncTask问题时.
请帮我解决这个难题.

提前致谢.

解决方法:

您无法在doInbackground中更新ui.你应该在ui线程上更新ui.在onPostExecute中设置适配器.

您也可以使用runOnUiThread.

显示联系人姓名.

public class MainActivity extends Activity {

    ListView lvCallList;
    ProgressDialog pd;
    ArrayList<String> aa = new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvCallList = (ListView) findViewById(R.id.list);

        new AsyncTask<Void, Void, Void>() 
        {
            @Override
            protected void onPreExecute() 
            {
                pd = ProgressDialog.show(MainActivity.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)
            {
                pd.dismiss();
             ArrayAdapter<String>   arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,aa);
             lvCallList.setAdapter(arrayAdapter);

            }//End of onPostExecute method
        }.execute((Void[]) null);
    }
        private void getContacts()
        {
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
            if (cur.getCount() > 0)
            {
                while (cur.moveToNext())
                {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    aa.add(name);
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                    {
                        Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                null,
                                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                                new String[]{id}, 
                                                null);
                        while (pCur.moveToNext())
                        {
                            String phoneNumber = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                          //  this.displayContactNumber.add(phoneNumber);
                        } 
                        pCur.close();
                    }
                }
            }
        }
    }

在我的手机上拍摄

编辑:2

public class MainActivity extends Activity {

    ListView lvCallList;
    ProgressDialog pd;
    ArrayList<String> aa = new ArrayList<String>();
    ArrayList<String> num= new ArrayList<String>();
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lvCallList = (ListView) findViewById(R.id.list);

        new AsyncTask<Void, Void, Void>() 
        {
            @Override
            protected void onPreExecute() 
            {
                pd = ProgressDialog.show(MainActivity.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)
            {
                pd.dismiss();
                CustomAdapter cus = new CustomAdapter(MainActivity.this);
               // ArrayAdapter<String>   arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,aa);
             lvCallList.setAdapter(cus);

            }//End of onPostExecute method
        }.execute((Void[]) null);
    }
        private void getContacts()
        {
            ContentResolver cr = getContentResolver();
            Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,null, null, null);
            if (cur.getCount() > 0)
            {
                while (cur.moveToNext())
                {
                    String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    aa.add(name);
                    if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
                    {
                        Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                                null,
                                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
                                                new String[]{id}, 
                                                null);
                        while (pCur.moveToNext())
                        {
                            String phoneNumber = pCur.getString(pCur.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER));
                            num.add(phoneNumber);
                        } 
                        pCur.close();
                    }
                }
            }
        }

        public class CustomAdapter extends BaseAdapter 
        {
            /*
             * Variables Declaration section
             */
            private Context mContext;

            public CustomAdapter(Context context) 
            {
                mContext = context;
            }//End of CustomAdapter constructor

            public int getCount() 
            {
                return aa.size();
            }//End of getCount method

            public Object getItem(int position) 
            {
                return position;
            }//End of getItem method

            public long getItemId(int position) 
            {
                return position;
            }//End of getItemId method

            public View getView(int position, View convertView, ViewGroup parent) 
            {
                ViewHolder holder;
                final int pos = position;

                if (convertView == null) 
                {
                    holder = new ViewHolder();

                    convertView = LayoutInflater.from(mContext).inflate(R.layout.display_contact, null);
                    holder.textviewName = (TextView) convertView.findViewById(R.id.textView1);
                    holder.textviewNumber = (TextView) convertView.findViewById(R.id.textView2);
                    holder.checkbox = (CheckBox) convertView.findViewById(R.id.checkBox1);

                    convertView.setTag(holder);
                }//End of if condition
                else
                {
                    holder = (ViewHolder) convertView.getTag();
                }//End of else

                holder.checkbox.setId(position);
                holder.textviewName.setId(position);
                holder.textviewNumber.setId(position);


                holder.textviewName.setText(aa.get(position));
                holder.textviewNumber.setText("No. "+num.get(position));

                holder.id = position;

                return convertView;
            }//End of getView method
        }//End of CustomAdapter instance inner class

        static class ViewHolder 
        {
            TextView textviewName;
            TextView textviewNumber;
            CheckBox checkbox;
            int id;
        }

}

标签:android,android-asynctask,android-contacts
来源: https://codeday.me/bug/20191007/1868045.html