File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on retrieving just only "Mobile" TYPE

 
ebi leong
Greenhorn
Posts: 4
  • 0
  • Mark post as helpful
  • send pies
  • 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);
}

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

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic