aspose file tools*
The moose likes Android and the fly likes Question on retrieving just only Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Question on retrieving just only "Mobile" TYPE" Watch "Question on retrieving just only "Mobile" TYPE" New topic
Author

Question on retrieving just only "Mobile" TYPE

ebi leong
Greenhorn

Joined: Jun 27, 2012
Posts: 4
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

Joined: Mar 07, 2010
Posts: 1632
    
    7

Please UseCodeTags. Readable code is much more likely to get responses.


[How To Ask Questions][Read before you PM me]
ebi leong
Greenhorn

Joined: Jun 27, 2012
Posts: 4
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
 
subject: Question on retrieving just only "Mobile" TYPE
 
Similar Threads
list view problem
listview with checkbox problem
how to create an search box and search an query
how to use tamil fonts in android mobile
styling items of a ListView