2012-01-10 10 views
5

मैं उस एप्लिकेशन पर काम कर रहा हूं जिसमें मुझे संपर्क पुस्तक और प्रदर्शन से सभी संपर्क लाने की आवश्यकता है। मैं चाहता हूं कि उपयोगकर्ता कुछ संपर्कों का चयन करें और उन्हें एक समूह में जोड़ें जो डीबी में सहेजा गया है।सिंक्रनाइज़ किए गए जीमेल खातों के साथ संपर्क नहीं ला सकते

मैं एक कस्टम सूची बनाई है view- contactitem.xml-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 


android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="horizontal"> 


<TextView 

    android:id="@+id/contactname" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:layout_weight="1" 
    android:layout_marginLeft="20dp" 
    android:ellipsize="end"   
    android:singleLine="true" 
    android:clickable="true"/> 

<TextView 
    android:id="@+id/contactnum" 
    android:layout_width="wrap_content" 
    android:textColor="@color/White" 
    android:clickable="true" 
    android:layout_gravity="center_vertical" 
    android:layout_height="wrap_content"/> 

    <Button 
    android:id="@+id/add" 
    android:layout_width="wrap_content" 
    android:layout_height="35dp" 
    android:text="@string/add_contacts" 
    android:layout_gravity="center_vertical" 
    android:layout_marginLeft="10dp" 
    android:layout_marginRight="10dp" 
    android:focusable="false" 
    android:focusableInTouchMode="false"/> 

</LinearLayout> 

मैं संपर्क से संपर्क लाना लिए एक SelectContact वर्ग

public class SelectContacts extends Activity implements OnClickListener{ 

    private String groupName; 
    private Button back; 
    private Button home; 
    private List<Contact> list = new ArrayList<Contact>(); 

    private ListView contactList; 

     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.selectcontacts); 

      Bundle bundle = getIntent().getExtras(); 
      groupName=bundle.getString("groupName"); 

      back=(Button)findViewById(R.id.back_button); 
      back.setOnClickListener(this); 

      home=(Button)findViewById(R.id.home_button); 
      home.setOnClickListener(this); 

      contactList=(ListView)findViewById(R.id.contactsListView); 

      ContentResolver cr = getContentResolver(); 



      Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 

      cur.moveToFirst(); 

      if (cur.getCount() > 0) { 

       do{ 

        String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 

        String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

        if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 

        String[] fields={ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};  

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, fields, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new String[]{id}, null); 

        pCur.moveToFirst(); 

        do { 

         String number=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

         Contact c=new Contact(name,number); 

         list.add(c); 

         }while (pCur.moveToNext()); 

         pCur.close(); 
        }    



        }while (cur.moveToNext()); 

       ContactAdapter contactAdapter=new ContactAdapter(this, R.layout.contactitem, list, contactList,groupName); 



       contactList.setAdapter(contactAdapter); 


      } 


     } 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Intent intent= new Intent(); 

      if(v==back){ 

       Bundle bundle = new Bundle(); 
       bundle.putString("groupName", groupName); 
       intent.putExtras(bundle); 

       intent.setClass(SelectContacts.this, GroupDetails.class); 
       startActivity(intent); 

      } 
      if(v==home){ 
       intent.setClass(SelectContacts.this, Welcome.class); 
       startActivity(intent); 

      } 

     } 
} 

पुस्तक और एक कस्टम लागू किए गए हों adapter- ContactAdapter- `

public class ContactAdapter extends ArrayAdapter<HashMap<String, Contact>>{ 

private ArrayList<HashMap<String, Contact>> items; 

private LayoutInflater mInflater ; 

public ContactAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Contact>> items) { 

     super(context, textViewResourceId, items); 

     this.items = items; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

      mInflater = LayoutInflater.from(getContext()); 

      ViewHolder holder; 

     if(convertView==null){ 

      convertView = mInflater.inflate(R.layout.contactitem, parent, false); 

      holder = new ViewHolder(); 

      holder.name = (TextView) convertView.findViewById(R.id.contactname); 

      holder.number = (TextView) convertView.findViewById(R.id.contactnum); 

      holder.add=(Button)convertView.findViewById(R.id.add); 

      convertView.setTag(holder); 

     } 

     else{ 

      holder=(ViewHolder)convertView.getTag(); 

     } 

     String name=items.get(position).get(""+position).getContactName(); 

     String number=items.get(position).get(""+position).getContactNum(); 

     holder.name.setText(name); 

     holder.number.setText(number); 

     return convertView; 
} 



static class ViewHolder{ 

    TextView name; 

    TextView number; 

    Button add; 

} 

}

here Contact is a simple POJO- 



public class Contact { 
    private String contactName; 
    private String contactNum; 

    public String getContactName() { 
     return contactName; 
    } 
    public void setContactName(String contactName) { 
     this.contactName = contactName; 
    } 

    public String getContactNum() { 
     return contactNum; 
    } 
    public void setContactNum(String contactNum) { 
     this.contactNum = contactName; 
    } 

} 

मैं एंड्रॉयड में एक नौसिखिया हूँ ..

ऊपर कोड emulater पर चुप ठीक काम करता है और संपर्क नाम और सूची में प्रदर्शित करने संख्या लाने। लेकिन जब मैं जो एक Gmail है, खाता सिंक्रनाइज़ की है, यह निम्न त्रुटि के साथ दुर्घटनाग्रस्त हो गया फोन पर इस कोड को ..

CursorIndexOutOfBoundsException का परीक्षण: सूचकांक 0 का अनुरोध किया, 0

के आकार के साथ मैं किसी भी विचार क्यों नहीं है यह हो रहा है। कृपया कुछ सुझाव दें ...

+0

क्या यह मानना ​​सुरक्षित है कि आपके पास मेनिफेस्ट में सही अनुमति सेट है? http://developer.android.com/reference/android/Manifest.permission.html#READ_CONTACTS – Joset

+1

umm मुझे लगता है कि मैंने मेनिफेस्ट में सभी आवश्यक अनुमतियां ठीक से सेट की हैं। जो निम्नानुसार हैं - <उपयोग-अनुमति एंड्रॉइड: नाम = "android.permission.READ_CONTACTS" /> <उपयोग-अनुमति एंड्रॉइड: नाम = "android.permission.GET_ACCOUNTS" /> क्या मुझे किसी अन्य अनुमति की आवश्यकता है ? –

+0

आपको यह त्रुटि कहां मिलती है? मुझे यकीन नहीं है लेकिन मुझे लगता है कि समस्या आपकी दूसरी क्वेरी में है। ऐसा कुछ ऐसा दिखता है: http://stackoverflow.com/questions/3370628/retrieve-contact-phone-number-from-uri-in-android – kingston

उत्तर

1

कोड के ऊपर आपको दिया गया है एंड्रॉइड संपर्क डेटा जीमेल या कोई अन्य संपर्क नहीं है क्योंकि आपका फोन आपके जीमेल संपर्क या किसी अन्य संपर्क के साथ सिंक है, केवल फोन संपर्क सभी जीमेल की अनुमति नहीं है या स्काइप संपर्क।

संबंधित मुद्दे