2010-12-03 30 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自动管理它。