• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Question on retrieving just only "Mobile" TYPE

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}

}
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags. Readable code is much more likely to get responses.
 
ebi leong
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic