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);
}

}

4

2 回答 2

0

您必须使用所有电子邮件、电话号码、网址等。

例子:

Linkify.addLinks(textView, Linkify.WEB_URLS);

  1. 参数:您要添加字符串的 textview
  2. 您要跟踪电子邮件、电话或网络的内容

更多详情:http: //developer.android.com/reference/android/text/util/Linkify.html

注意:为此,您无需实现任何 onClick 等。Linkif 会自动管理它。

于 2014-11-06T07:42:44.270 回答
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 行的查询中访问信息,则会出现异常。请阅读该错误并显示它。

于 2010-12-03T18:13:04.363 回答