2013-11-21 48 views
0

我已經創建了一個小程序來查看列表視圖中的聯繫人姓名和號碼。用意向查看聯繫方式

public class showcontacts extends Activity 
{ 
    ListView lv; 
    Uri u; 
    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listcontacts); 
    lv=(ListView)findViewById(R.id.listView1); 
    lv.setOnItemClickListener(new mysellisten()); 
    u=Phone.CONTENT_URI; 


    shownames(); 
} 


public void shownames() 
{ 
    String[] p=new String[] { 
       Contacts._ID, 
      Contacts.DISPLAY_NAME, 
      Contacts.HAS_PHONE_NUMBER, 
      Phone.NUMBER 

      }; 
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" 
+ ("1") + "' and "+ContactsContract.Contacts.HAS_PHONE_NUMBER+"='1'" ; 
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME 
+ " COLLATE LOCALIZED ASC"; 
Cursor c=getContentResolver().query(u, p, selection,null, sortOrder); 
String from[]=new String[]{ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER}; 
int id[]={R.id.name,R.id.phoneno}; 
SimpleCursorAdapter sc=new SimpleCursorAdapter(this,R.layout.phoneview, c, from, id); 

lv.setAdapter(sc); 

} 


class mysellisten implements OnItemClickListener 
{ 


    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     Toast.makeText(getApplicationContext(), "called", Toast.LENGTH_SHORT).show(); 
     Uri nu=ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, arg3); 
     Intent i=new Intent(Intent.ACTION_VIEW,nu); 
     startActivity(i); 

    } 

} 

phoneview是一個簡單的XML與Imageview和2 textviews。 我能夠得到名稱和phonenos.However單擊列表視圖中的項目顯示的記錄是不正確的。它不匹配我單擊的記錄。某些記錄甚至不會打開模擬器顯示空白屏幕後返回到列表視圖。

此外,爲什麼我能夠在使用Phone.CONTENT_URI時使用Contacts表列,但如果使用Contacts.CONTENT_URI,我無法使用Phone.NUMBER字段。 KINDLY更新。

回答

0

您是否已將聯繫人permission添加到AndroidManifest.xml?

<uses-permission android:name="android.permission.READ_CONTACTS" /> 
<uses-permission android:name="android.permission.WRITE_CONTACTS" /> 
+0

是不然爲什麼它會顯示聯繫人的編號電話號碼。 – user2779311

0

你絕對可以retrive聯繫人姓名和使用Contacts.CONTENT_URI請找,做它

try { 
     Cursor cursor = getContentResolver().query(
       ContactsContract.Contacts.CONTENT_URI, null, null, null, 
       Phone.DISPLAY_NAME + " ASC"); 
     while (cursor.moveToNext()) { 
      String contactId = cursor.getString(cursor 
        .getColumnIndex(ContactsContract.Contacts._ID)); 
      String hasPhone = cursor 
        .getString(cursor 
          .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
      String name = cursor 
        .getString(cursor 
          .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

      if ("1".equals(hasPhone) || Boolean.parseBoolean(hasPhone)) { 
       // You know it has a number so now query it like this 
       Cursor phones = getContentResolver().query(
         ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
         null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
           + " = " + contactId, null, null); 
       while (phones.moveToNext()) { 
        String dname = cursor 
          .getString(cursor 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        System.out.println("Name is " + dname); 
        String phoneNumber = phones 
          .getString(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        System.out.println("Phone Number is " + phoneNumber); 
        int itype = phones 
          .getInt(phones 
            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 

        final boolean isMobile = itype == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE 
          || itype == ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE; 


       } 
       phones.close(); 
      } 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

我的問題不是如何獲取這些數據,而是爲什麼我的代碼是這樣工作的,如果可能的話回答下面的部分 – user2779311