这是我的代码: ...................
public class GetContactsDemo extends Activity {
Button btnImport,btnAdd;
ListView list;
LazyAdapter adap;
ArrayList<String> Cnames = new ArrayList<String>();
ArrayList<String> CphoneNo = new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnImport = (Button)findViewById(R.id.btnimport);
btnAdd = (Button)findViewById(R.id.btnAdd);
list = (ListView)findViewById(R.id.listView1);
//to import contacts
btnImport.setOnClickListener(new OnClickListener() {![enter image description here][2]
public void onClick(View v)
{
readContacts();
adap = new LazyAdapter(getApplicationContext());
list.setAdapter(adap);
}
});
//to add contact
btnAdd.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
Intent addcontact = new Intent(getApplicationContext(),AddingNewContactActivity.class);
startActivity(addcontact);
}
});
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3)
{
Toast.makeText(getApplicationContext(), ""+arg2, 1000).show();
Uri uri = Uri.parse("com.android.contacts/raw_contacts/2");
Intent i = new Intent(Intent.ACTION_SEND);
// i.setType("image/jpeg");
i.setType("text/x-vcard");
i.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(i, "Send Contact"));
}
});
}
public void readContacts(){
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
null, null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
System.out.println("name : " + name + ", ID : " + id);
Log.e("name","name-->"+name);
Cnames.add(name);
// get the phone number
Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",
new String[]{id}, null);
while (pCur.moveToNext()) {
String phone = pCur.getString(
pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
System.out.println("phone" + phone);
Log.e("phone","phone-->"+phone);
CphoneNo.add(phone);
}
pCur.close();
class LazyAdapter extends BaseAdapter
{
Context mcontext;
LayoutInflater inflater = null;
public LazyAdapter(Context context) {
this.mcontext = context;
inflater = (LayoutInflater) mcontext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount()
{
return Cnames.size();
}
public Object getItem(int arg0)
{
return null;
}
public long getItemId(int arg0)
{
return arg0;
}
public View getView(int position, View v, ViewGroup arg2)
{
View vi = v;
vi = inflater.inflate(R.layout.inflate, null);
TextView txt = (TextView) vi.findViewById(R.id.txtname);
TextView txt1 = (TextView) vi.findViewById(R.id.txtphoneno);
txt.setText(Cnames.get(position));
txt1.setText(CphoneNo.get(position));
return vi;
}
}
}
提前致谢