posted 5 years ago
Hi there, I am doing a android project on retrieve contacts and i have code out on retrieving contacts from the android phone, but currently i want to implement it to just retrieve ONLY "Mobile" TYPE numbers and i would like to seek help on my issue and hope someone can guide me to show some examples.
This is my following Activity code:
String contactNumber = null;
String nameOfContact = null;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContactsBean> pc1 = getContactNumbers(this);
ArrayAdapter<ContactsBean> adapter = new ContactAdapter(this, R.layout.contactlistitem, pc1);
lv = (ListView)findViewById(R.id.contactList);
lv.setAdapter(adapter);
}
public ArrayList<ContactsBean> getContactNumbers(Context context){
String contactNumber = null;
ArrayList<ContactsBean> phoneContacts = new ArrayList<ContactsBean>();
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 nameOfContact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_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()) {
// Do something with phones
contactNumber = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
phoneContacts.add(new ContactsBean(nameOfContact, contactNumber));
}
pCur.close();
}
}
}
cur.close();
return phoneContacts;
}
And this is my android Custom Adapter:
public class ContactAdapter extends ArrayAdapter<ContactsBean>{
private Context context;
private int layoutResourceId;
private List<ContactsBean> contactList;
public ContactAdapter(Context context, int layoutResourceId, List<ContactsBean> contactList){
super(context,layoutResourceId,contactList);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.contactList = contactList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, parent, false);
ContactHolder contactHolder = new ContactHolder();
contactHolder.txtName = (TextView)view.findViewById(R.id.txtDisplayName);
contactHolder.txtPhone = (TextView)view.findViewById(R.id.txtPhoneNo);
contactHolder.setContact(contactList.get(position));
view.setTag(contactHolder);
}
return view;
}
static class ContactHolder{
TextView txtName;
TextView txtPhone;
ContactsBean contactList;
protected void setContact(ContactsBean contact)
{
txtName.setText(contact.getNameOfContact());
txtPhone.setText(contact.getContactNumber());
contactList = contact;
}
protected ContactsBean getContact() {
return contactList;
}
}
@Override
public ContactsBean getItem(int position)
{
return contactList.get(position);
}
}
This is my following Activity code:
String contactNumber = null;
String nameOfContact = null;
private ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<ContactsBean> pc1 = getContactNumbers(this);
ArrayAdapter<ContactsBean> adapter = new ContactAdapter(this, R.layout.contactlistitem, pc1);
lv = (ListView)findViewById(R.id.contactList);
lv.setAdapter(adapter);
}
public ArrayList<ContactsBean> getContactNumbers(Context context){
String contactNumber = null;
ArrayList<ContactsBean> phoneContacts = new ArrayList<ContactsBean>();
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 nameOfContact = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_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()) {
// Do something with phones
contactNumber = pCur.getString(pCur.getColumnIndex(Phone.NUMBER));
phoneContacts.add(new ContactsBean(nameOfContact, contactNumber));
}
pCur.close();
}
}
}
cur.close();
return phoneContacts;
}
And this is my android Custom Adapter:
public class ContactAdapter extends ArrayAdapter<ContactsBean>{
private Context context;
private int layoutResourceId;
private List<ContactsBean> contactList;
public ContactAdapter(Context context, int layoutResourceId, List<ContactsBean> contactList){
super(context,layoutResourceId,contactList);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.contactList = contactList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View view = convertView;
if(view == null)
{
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
view = inflater.inflate(layoutResourceId, parent, false);
ContactHolder contactHolder = new ContactHolder();
contactHolder.txtName = (TextView)view.findViewById(R.id.txtDisplayName);
contactHolder.txtPhone = (TextView)view.findViewById(R.id.txtPhoneNo);
contactHolder.setContact(contactList.get(position));
view.setTag(contactHolder);
}
return view;
}
static class ContactHolder{
TextView txtName;
TextView txtPhone;
ContactsBean contactList;
protected void setContact(ContactsBean contact)
{
txtName.setText(contact.getNameOfContact());
txtPhone.setText(contact.getContactNumber());
contactList = contact;
}
protected ContactsBean getContact() {
return contactList;
}
}
@Override
public ContactsBean getItem(int position)
{
return contactList.get(position);
}
}
ebi leong
Greenhorn
Posts: 4
posted 5 years ago
Sorry about that mistake of not using the code tag. Resend: I am doing a android project on retrieve contacts and i have code out on retrieving contacts from the android phone, but currently i want to implement it to just retrieve ONLY "Mobile" TYPE numbers and i would like to seek help on my issue and hope someone can guide me to show some examples.
This is my following Activity code:
And this is my android Custom Adapter:
This is my following Activity code:
And this is my android Custom Adapter:
