我正在尝试列出移动设备中的所有联系人。到目前为止一切正常,但是当我想到在其中运行我的代码时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
displayContactName
使用断点的值pd.dismiss();
是
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
问题就来了。请帮我解决这个难题。
提前致谢。