如何使用 AsyncTask 在 Android 中显示联系人? [英] How to display contacts in Android using AsyncTask?
问题描述
我正在尝试列出移动设备中的所有联系人.到目前为止一切正常,但是当我想到在 AsyncTask
中运行我的代码时,我在屏幕上什么也没有看到.
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 screen.
这是代码片段
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
value using break point at pd.dismiss();
is
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
时,问题就来了.请帮我解决这个难题.
I don't know what's wrong in my code, when I don't use AsyncTask
then my code works perfectly fine, but when I use AsyncTask
problem comes.
Please help me to solve this puzzle.
提前致谢.
推荐答案
我发现了问题,我试图在设置适配器后初始化 count
,这就是问题所在,感谢 stackoverflow 和 @Raghunandan
I found out the problem, I was trying to initialise count
after setting adapter, that was the problem, Thanks stackoverflow and @Raghunandan
protected void onPostExecute(Void result)
{
count = displayContactName.size();
thumbnailsselection = new boolean[count];
lvCallList.setAdapter(new CustomAdapter(CallListActivity.this));
pd.dismiss();
}//End of onPostExecute method
这篇关于如何使用 AsyncTask 在 Android 中显示联系人?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!