Android - contact picker in fragment -


i have viewpager/sliding tabs. want able 4 contacts address book on phone , save them in app later use. have code contact picker activity contact page fragment , have yet work.

any ideas or suggestions appreciated!

code main activity connected to:

import android.os.bundle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.app.fragmenttransaction; import android.support.v4.view.viewpager; import android.support.v7.app.actionbar; import android.support.v7.app.actionbaractivity; import android.view.view; import android.widget.button; import android.widget.edittext; import android.widget.textview;    public class mainactivity2activity extends actionbaractivity implements actionbar.tablistener, viewpager.onpagechangelistener {  private viewpager mviewpager;   @override protected void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main_activity2);       pageradapter adapter = new pageradapter(getsupportfragmentmanager());     mviewpager = (viewpager) findviewbyid(r.id.pager);     mviewpager.setadapter(adapter);      mviewpager.setonpagechangelistener(this);      actionbar actionbar = getsupportactionbar();     actionbar.setnavigationmode(actionbar.navigation_mode_tabs);      actionbar.tab tab = actionbar.newtab().settext("contacts").settablistener(this);     actionbar.addtab(tab);      tab = actionbar.newtab().settext("gps").settablistener(this);     actionbar.addtab(tab);      tab = actionbar.newtab().settext("message").settablistener(this);     actionbar.addtab(tab);  }   public class pageradapter extends fragmentpageradapter {      public pageradapter(fragmentmanager fm) {         super(fm);     }      public fragment getitem(int arg0) {         switch (arg0) {             case 0:                 return new mainmenu_contacts();             case 1:                 return new mainmenu_gps();             case 2:                 return new mainmenu_message();             default:                 return null;         }     }     public int getcount() {         return 3;     } }  //implements on pager selected @override public void onpagescrolled(int i, float v, int i2) {  }  @override public void onpageselected(int i) {     getsupportactionbar().setselectednavigationitem(i); }  @override public void onpagescrollstatechanged(int i) {  }   //implements tab listener @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) {     mviewpager.setcurrentitem(tab.getposition()); }  @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) {  }  @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) {  } } 

xml main activity:

<?xml version="1.0" encoding="utf-8"?> <android.support.v4.view.viewpager xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="match_parent" /> 

code contact fragment:

import android.app.activity; import android.content.intent; import android.database.cursor; import android.net.uri; import android.os.bundle; import android.provider.contactscontract; import android.support.v4.app.fragment;  import android.view.view;  import android.widget.button;  import android.view.view.onclicklistener;    public class mainmenu_contacts extends fragment implements onclicklistener {  private button mbtncontacts; private final int pick = 1;  /** called when activity first created. */ @override public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.activity_main_menu__contacts);     mbtncontacts = (button) findviewbyid(r.id.xbtncontacts);     mbtncontacts.setonclicklistener(this); }  @override public void onclick(view v) {     // todo auto-generated method stub     // opening contacts window window     intent intent = new intent(intent.action_pick,             contactscontract.contacts.content_uri);     // calling onactivityresult intenet , conatct identifie     startactivityforresult(intent, pick); }  @override public void onactivityresult(int reqcode, int resultcode, intent data) {     super.onactivityresult(reqcode, resultcode, data);     switch (reqcode) {         case (pick):             if (resultcode == activity.result_ok) {                 uri contactdata = data.getdata();                 cursor c = managedquery(contactdata, null, null, null,     null);                 if (c.movetofirst()) {                     string name = c.getstring(c                             .getcolumnindexorthrow(contacts.people.name));                     // todo whatever want selected     contact                     // name.                 }             }             break;     } } 

xml contact:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <button android:layout_width="fill_parent"     android:layout_height="wrap_content" android:id="@+id/xbtncontacts"     android:text="pick contacts" /> </linearlayout> 

i used contact picker following steps. can change imagebutton view class

imagebutton pick_contact = (imagebutton) findviewbyid(r.id.pick_contact); pick_contact.setonclicklistener(new view.onclicklistener() {     @override     public void onclick(view view) {         intent intent = new intent(intent.action_pick, contactscontract.commondatakinds.phone.content_uri);         startactivityforresult(intent, pick_contact);     } }); 

later in activity:

@override protected void onactivityresult(final int requestcode, int resultcode, intent data) {      if (data != null) {         uri result = data.getdata();          cursor c = getcontentresolver().query(                 contactscontract.commondatakinds.phone.content_uri, null,                 contactscontract.commondatakinds.phone._id + "=?",                 new string[]{result.getlastpathsegment()}, null);          if (c.getcount() >= 1 && c.movetofirst()) {              final string number = c.getstring(c.getcolumnindex(contactscontract.commondatakinds.phone.number));             final string name = c.getstring(c.getcolumnindex(contactscontract.commondatakinds.phone.display_name));         }     } } 

important: in androidmanifest.xml, add

<uses-permission android:name="android.permission.read_contacts" /> 

update: take at: https://github.com/codinguser/android_contact_picker


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -