开发者

how to get contact number into Text view?

this is is the code i have but everytime i click on the contact it force closes. and is there a code so that when i get the contact it adds it into a text view?

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.Conta开发者_StackOverflow中文版ctList);
    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);
}

}


based on my experience with the contacts list, you need to design your query based on what is available. In 1.6 there was the simplicity of one table with all the information. However; with the dawn of 2.0, they introduced two tables. Where you get the ID from one table and the query based on this ID to find the phone number. To illustrate this here is a piece of sample code that worked for me, although i'm having some minor problems where some contacts won't return a phone number 2/70 although all 70 users have an ID and Phone number. I hope it helps:

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

If you have any further questions, please don't hesitate to ask, I'll do my best to answer them. For what I can tell without a log cat is that you are attempting a look up of the phone number the proper table structure for the query. If you try to access information from a query that returned 0 rows, then you'll get an exception. Please read that error and display it.


You have to use for all Email, Phone Numbers, Web-Address etc.

Example:

Linkify.addLinks(textView, Linkify.WEB_URLS);

  1. Parameter: textview which you are adding string
  2. Which thing you want to track email,phone or web

For more details: http://developer.android.com/reference/android/text/util/Linkify.html

Note: for this you no need to implement any onClick etc. Linkif automatically manage it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜