如何编辑从上下文菜单中接触 [英] How to Edit contact from context Menu
本文介绍了如何编辑从上下文菜单中接触的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在开发一个联系人的应用程序,它允许:通话,短信,delele等..和编辑联系。当用户点击并按住一个联系人,上下文菜单显示(下图)。该行动打电话,短信,......已经完成,但编辑是没有的。请给我一些code或建议。在此先感谢
@覆盖
公共布尔onContextItemSelected(菜单项项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();
开关(item.getItemId()){
案例R.id.call:
//做一点事
字符串mPhoneNumber =电话:+ getPhoneNumber(mRecordId);
意向意图=新的意图(Intent.ACTION_CALL,Uri.parse(mPhoneNumber));
startActivity(意向);
返回true;
案例R.id.message:
//做一点事
字符串mSmsNumber = getPhoneNumber(mRecordId);
startActivity(新意图(Intent.ACTION_VIEW,Uri.parse(短信:+ mSmsNumber)));
返回true;
案例R.id.sendemail:
//做一点事
返回true;
案例R.id.edit:
// //做需要帮助的东西???
返回true;
案例R.id.delete:
//做一点事
新AlertDialog.Builder(mContext).setMessage(你要删除这个联系人?)
.setTitle(删除)。setCancelable(假).setPositiveButton(删除,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
deleteContactEntry(mRecordId);
populateContactList();
}
})。setNegativeButton(取消,新DialogInterface.OnClickListener(){
@覆盖
公共无效的onClick(DialogInterface对话,诠释它){
// TODO自动生成方法存根
dialog.cancel();
}
})。显示();
返回true;
默认:
返回super.onContextItemSelected(项目);
}
下面是我的方法使用:
/ **
*填充基于当前帐户中的离心器选择的帐户的联系人列表。
* /
私人无效populateContactList(){
//构建适配器的联系人条目
光标光标= getContacts();
的String []字段=新的String [] {
ContactsContract.Data.DISPLAY_NAME
};
SimpleCursorAdapter适配器=新SimpleCursorAdapter(这一点,R.layout.contact_entry,光标,
领域,新的INT [] {R.id.contactEntryText});
mContactList.setAdapter(适配器);
}
/ **
*获取当前选定的帐户的联系人列表。
*
返回:光标用于访问联系人列表。
* /
私人光标getContacts()
{
//运行查询
开放的我们的uri = ContactsContract.Contacts.CONTENT_URI;
的String []投影=新的String [] {
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME
};
串选择= ContactsContract.Contacts.IN_VISIBLE_GROUP +=+
(mShowInvisible0:1?)+';
//串选择= ContactsContract.Contacts.IN_VISIBLE_GROUP += 0;
的String [] selectionArgs两个= NULL;
字符串排序顺序= ContactsContract.Contacts.DISPLAY_NAME +分页中局部ASC;
返回managedQuery(URI,投影,选择,selectionArgs两个,排序顺序);
}
/ **
*启动ContactAdder活动添加新的联系人到选定accont。
* /
保护无效launchContactAdder(){
意图I =新的意图(这一点,ContactAdder.class);
startActivity(ⅰ);
}
/ **
*获取电话号码
* /
私人字符串getPhoneNumber(长的ContactID){
字符串mPhoneNumber = NULL;
的String [] colums =新的String [] {ContactsContract.CommonDataKinds.Phone.NUMBER};
字符串,其中= ContactsContract.Data.RAW_CONTACT_ID +=;
的String [] whereParameters =新的String [] {Long.toString(的ContactID)};
光标接触= getContentResolver()查询(ContactsContract.Data.CONTENT_URI,colums,其中,whereParameters,空)。
如果(contacts.moveToFirst()){
mPhoneNumber = contacts.getString(0);
}
contacts.close();
返回mPhoneNumber;
}
/ **
*删除联系人
* /
私人无效deleteContactEntry(长的ContactID){
//的String []投影=新的String [] {ContactsContract.RawContacts._ID};
字符串mSelectionClause = ContactsContract.RawContacts._ID +=?;
的String [] mSelectionArgs =新的String [] {Long.toString(的ContactID)};
。getContentResolver()删除(ContactsContract.RawContacts.CONTENT_URI,mSelectionClause,mSelectionArgs);
}
解决方案
有关编辑功能,您可以使用编辑意图调用默认的联系人应用程序
意向书我=新的意图(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI +/+的id)); //联系人的ID
startActivityForResult(ⅰ,idEDIT_CONTACT);
I have been developing a contact app, it allows : call, sms, delele, etc ... and Edit Contact. When user click and hold on a contact, a context menu show (picture below). The actions call, sms, ... was completed but Edit is n't. Please give me some code or advice . Thanks in advance
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.call:
//do something
String mPhoneNumber = "tel:" + getPhoneNumber(mRecordId);
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(mPhoneNumber));
startActivity(intent);
return true;
case R.id.message:
//do something
String mSmsNumber = getPhoneNumber(mRecordId);
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("sms:" + mSmsNumber)));
return true;
case R.id.sendemail:
//do something
return true;
case R.id.edit:
//do something // need help???
return true;
case R.id.delete:
//do something
new AlertDialog.Builder(mContext).setMessage("Do you want to delete this contact?")
.setTitle("Delete").setCancelable(false).setPositiveButton("Delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
deleteContactEntry(mRecordId);
populateContactList();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).show();
return true;
default:
return super.onContextItemSelected(item);
}
Here are methods i used:
/**
* 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.contact_entry, cursor,
fields, new int[] {R.id.contactEntryText});
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 selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = 0";
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 accont.
*/
protected void launchContactAdder() {
Intent i = new Intent(this, ContactAdder.class);
startActivity(i);
}
/**
* Get Phone Number
*/
private String getPhoneNumber(long contactId){
String mPhoneNumber = null;
String [] colums = new String[] { ContactsContract.CommonDataKinds.Phone.NUMBER };
String where = ContactsContract.Data.RAW_CONTACT_ID + "=?";
String[] whereParameters = new String[]{Long.toString(contactId)};
Cursor contacts = getContentResolver().query(ContactsContract.Data.CONTENT_URI,colums,where, whereParameters, null);
if (contacts.moveToFirst()) {
mPhoneNumber = contacts.getString(0);
}
contacts.close();
return mPhoneNumber;
}
/**
* Delete Contact
*/
private void deleteContactEntry(long contactId){
//String [] projection = new String [] {ContactsContract.RawContacts._ID};
String mSelectionClause = ContactsContract.RawContacts._ID + "=?";
String [] mSelectionArgs = new String [] {Long.toString(contactId)};
getContentResolver().delete(ContactsContract.RawContacts.CONTENT_URI, mSelectionClause, mSelectionArgs);
}
解决方案
For Edit function you can use Edit intent to call default contact app
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(Uri.parse(ContactsContract.Contacts.CONTENT_LOOKUP_URI + "/" + id)); //contact's id
startActivityForResult(i, idEDIT_CONTACT);
For sending Email have a look at this question or this
这篇关于如何编辑从上下文菜单中接触的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文