Paste
Pasted as Java by kalai ( 13 years ago )
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
listView.setOnItemClickListener(this);
contactSearch = (EditText) findViewById(R.id.contact_search);
contactSearch.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence cs, int start, int before,
int count) { }
public void beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
}
public void afterTextChanged(Editable arg0) {
getContactList(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME
+ " like '"
+ "%"
+ contactSearch.getText().toString()
+ "%'");
}
});
getContactList(null);
}
public void onItemClick(AdapterView<?> listview, View v, int position,
long id) {
ContactBean bean = (ContactBean) listview.getItemAtPosition(position);
showCallDialog(bean.getName(), bean.getPhoneNo());
}
private void showCallDialog(String name, final String phoneNo) {
new AlertDialog.Builder(ContactListActivity.this)
.setMessage("Are you sure want to call " + name + " ?")
.setTitle("Call?")
.setCancelable(true)
.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
String _phoneNumber = "tel:" + phoneNo;
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse(_phoneNumber));
startActivity(intent);
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).show();
}
private void getContactList(String selection) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
selection, null, null);
phones.moveToFirst();
ContactBean objContact;
if (!list.isEmpty()) {
list.clear();
}
while (!phones.isAfterLast()) {
objContact = new ContactBean();
String name = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones
.getString(phones
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
objContact.setName(name);
objContact.setPhoneNo(phoneNumber);
list.add(objContact);
phones.moveToNext();
}
phones.close();
objAdapter = new ContactsAdapter(ContactListActivity.this,
R.layout.alluser_row, list);
listView.setAdapter(objAdapter);
}
}
Revise this Paste