2010-12-03 65 views
0

這是我有的代碼,但每次我點擊它強制關閉的聯繫人。並有代碼,以便當我得到聯繫它將其添加到文本視圖?如何將聯繫人編號導入文本視圖?

public static final String TAG =「ContactManager」;

private Button mAddAccountButton; 
private ListView mContactList; 
private boolean mShowInvisible; 
private CheckBox mShowInvisibleControl; 

/** 
* Called when the activity is first created. Responsible for initializing the UI. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    Log.v(TAG, "Activity State: onCreate()"); 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main2); 

    // Obtain handles to UI objects 
    mAddAccountButton = (Button) findViewById(R.id.AddContact); 
    mContactList = (ListView) findViewById(R.id.ContactList); 
    mShowInvisibleControl = (CheckBox) findViewById(R.id.ShowInvisible); 

    // Initialize class properties 
    mShowInvisible = false; 
    mShowInvisibleControl.setChecked(mShowInvisible); 

    // Register handler for UI elements 
    mAddAccountButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      Log.d(TAG, "mAddAccountButton clicked"); 
      launchContactAdder(); 
     } 
    }); 
    mShowInvisibleControl.setOnCheckedChangeListener(new OnCheckedChangeListener() { 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      Log.d(TAG, "mShowInvisibleControl changed: " + isChecked); 
      mShowInvisible = isChecked; 
      populateContactList(); 
     } 
    }); 

    // Populate the contact list 
    populateContactList(); 
} 

/** 
* Populate the contact list based on account currently selected in the account spinner. 
*/ 
private void populateContactList() { 
    // Build adapter with contact entries 
    Cursor cursor = getContacts(); 
    String[] fields = new String[] { 
      ContactsContract.Data.DISPLAY_NAME 
    }; 
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.main, cursor, 
      fields, new int[] {R.id.TextView01}); 
    mContactList.setAdapter(adapter); 
} 

/** 
* Obtains the contact list for the currently selected account. 
* 
* @return A cursor for for accessing the contact list. 
*/ 
private Cursor getContacts() 
{ 
    // Run query 
    Uri uri = ContactsContract.Contacts.CONTENT_URI; 
    String[] projection = new String[] { 
      ContactsContract.Contacts._ID, 
      ContactsContract.Contacts.DISPLAY_NAME 
    }; 
    String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + 
      (mShowInvisible ? "0" : "1") + "'"; 
    String[] selectionArgs = null; 
    String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC"; 

    return managedQuery(uri, projection, selection, selectionArgs, sortOrder); 

} 

/** 
* Launches the ContactAdder activity to add a new contact to the selected account. 
*/ 
protected void launchContactAdder() { 
    Intent i = new Intent(this,Class1.class); 
    startActivity(i); 
} 

}

+0

請提供一個logcat。 – 2010-12-03 17:54:59

+1

很容易忘記 – Kennet 2010-12-03 19:57:02

回答

0

根據我的聯繫人列表中的經驗,你需要基於什麼是可用來設計您的查詢。在1.6版本中,所有信息都是簡單的一張表。然而;隨着2.0的曙光,他們推出了兩張桌子。您從哪裏獲取某個表的ID並根據此ID查詢該電話號碼。爲了說明這一點,這裏是一段示例代碼,雖然我有一些小問題,但有些聯繫人不會返回電話號碼2/70,儘管所有70個用戶都有一個ID和電話號碼。我希望它有幫助:

// look up contact via name 

      String name = contacts.getItem(arg1); 
    Uri lookup = Uri.withAppendedPath(
      ContactsContract.Contacts.CONTENT_FILTER_URI, name); 

    // look up id 
    Cursor c = getContentResolver().query(lookup, null, null, null, null); 
    String id = null; 
    int id_index = c.getColumnIndexOrThrow(ContactsContract.Contacts._ID); 
    if (c.moveToFirst()) 
     id = c.getString(id_index); 
    else 
     Toast.makeText(getApplicationContext(), "Friend not found", 
       Toast.LENGTH_SHORT).show(); 
    c.close(); 

    // use id if not null, to find contact's phone number/display name 
    if (id != null) { 
     String where = ContactsContract.Data.CONTACT_ID + " = " + id 
       + " AND " + ContactsContract.Data.MIMETYPE + " = '" 
       + ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE 
       + "'"; 

     c = getContentResolver().query(ContactsContract.Data.CONTENT_URI, 
       null, where, null, null); 

     c.moveToFirst(); 

     int iname = c 
       .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME); 
     int iphone = c 
       .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER); 

     if (c.getCount() > 0) { 
      _friend.setName(c.getString(iname)); 
      _friend.setPhone(c.getString(iphone)); 

如果您還有其他問題,請不要猶豫,問我,我會盡我所能來回答他們。我可以告訴沒有登錄貓的是,你正在嘗試查詢電話號碼查詢的適當表結構。如果您嘗試從返回0行的查詢中訪問信息,則會發生異常。請閱讀該錯誤並進行顯示。

0

你必須使用所有的電子郵件,電話號碼,網絡地址等

例子:

Linkify.addLinks(TextView的,Linkify.WEB_URLS);

  1. 參數:TextView的其中添加字符串
  2. 你要哪件事追蹤電子郵件,電話或網絡

有關詳細信息: http://developer.android.com/reference/android/text/util/Linkify.html

注:這個你沒有需要實現任何onClick等Linkif自動管理它。