乐者为王

Do one thing, and do it well.

Android下显示本地Contact Number信息(续)

前文的代码只能显示联系人的第一个号码,很不方便。如果要列出联系人的所有号码,需要将People.CONTENT_URI改为Phones.CONTENT_URI。具体代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_list);

        ListView contactList = (ListView)findViewById(R.id.contactList);

        Cursor contactsCursor = managedQuery(Phones.CONTENT_URI,
                null, null, null, null);
        startManagingCursor(contactsCursor);

        String[] columnsToMap = new String[] {Phones.NAME, Phones.NUMBER};
        int[] mapTo = new int[] {R.id.contactName, R.id.contactNumber};

        SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this,
                R.layout.contact_entry, contactsCursor,
                columnsToMap, mapTo);
        contactList.setAdapter(mAdapter);
    }
}

Comments