package com.yarin.Android.my contacts;
导入安卓. app.list activity;
import Android.content.component name;
导入安卓. content.content URIs;
导入安卓. content.intent;
导入安卓. database.cursor;
导入安卓. net.uri;
导入安卓. OS.bundle;
导入安卓. util.log;
导入安卓. view.context menu;
导入安卓. view.menu;
import android.view.MenuItem;
导入安卓. view.view;
导入Android.view.context menu.context menu info;
import Android .构件. adapter view;
import Android .构件. listview;
import Android .构件. simplecursoradapter;
publicclassmycontactsextendslistactivity
{
私有身份证明=' my contacts ';
privatestaticfinalintaddcontact _ id=menu.first;
privatestaticfinalinteditcontact _ id=menu.first 1;
privatestaticfinalintdelecontact _ id=menu.first 2;
privatestaticfinalintexitcontact _ id=menu.first 3;
公共void oncreate (绑定验证例程状态) )。
{
super.oncreate (savedinstancestate;
setdefaultkeymode (default _ keys _ shortcut;
Intent intent=getIntent (;
if(intent.getdata(==null ) {
log.v(tag,' getdata null ';
intent.setdata (contacts provider.content _ uri );
}
getListView ().setoncreatecontextmenulistener ) ) this;
getListView ().setbackgroundresource (r.drawable.BG );
log.v(tag,' oncreate ' );
cursor cursor=管理的查询(get intent (.get data ),ContactColumn.PROJECTION,null,null,null );
//登录各列表显示格式:姓名手机
simplecursoradapteradapter=newsimplecursoradapter (this,
Android.r.layout.simple _ list _ item _ 2,
cursor、
new String[] {ContactColumn.NAME,ContactColumn.MOBILENUM },
new int[] { android.R.id.text1,android.R.id.text2 };
设置列表适配器(适配器;
}
公共蓝牙创建选项菜单(菜单菜单) )。
{
super.oncreateoptionsmenu(menu;
log.v(tag,' onCreateOptionsMenu ';
//添加联系人
menu.add(0,AddContact_ID,0,R.string.add_user ) )。
. setshortcut('3',' a ' )。
. seticon(r.Drawable.add );
> //动态添加菜单技术Intent intent = new Intent(null, getIntent().getData());
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
new ComponentName(this, MyContacts.class), null, intent, 0, null);
//退出程序
menu.add(0, EXITContact_ID, 0, R.string.exit)
.setShortcut('4', 'd')
.setIcon(R.drawable.exit);
return true;
}
//处理菜单操作
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case AddContact_ID:
//添加联系人
startActivity(new Intent(Intent.ACTION_INSERT, getIntent().getData()));
return true;
case EXITContact_ID:
//退出程序
this.finish();
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onPrepareOptionsMenu(Menu menu)
{
super.onPrepareOptionsMenu(menu);
final boolean haveItems = getListAdapter().getCount() > 0;
Log.v(TAG, "onPrepareOptionsMenu");
if (haveItems)
{
Uri uri = ContentUris.withAppendedId(getIntent().getData(), getSelectedItemId());
Log.v(TAG, uri.toString());
Intent[] specifics = new Intent[2];
specifics[0] = new Intent(Intent.ACTION_EDIT, uri);
specifics[1] = new Intent(Intent.ACTION_VIEW, uri);
MenuItem[] items = new MenuItem[2];
//添加满足条件的菜单,当按下菜单之后,intent的是specifics[0] 和 specifics[1]要启动的activity
Intent intent = new Intent(null, uri);
intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0, null, specifics, intent, 0, items);
if (items[0] != null)
{
//编辑联系人
items[0].setShortcut('1', 'e').setIcon(R.drawable.edituser).setTitle(R.string.editor_user);
}
if (items[1] != null)
{
//查看联系人
items[1].setShortcut('2', 'f').setTitle(R.string.view_user).setIcon(R.drawable.viewuser);
}
}
else
{//没有项目即去掉添加的菜单
menu.removeGroup(Menu.CATEGORY_ALTERNATIVE);
}
return true;
}
//动态菜单处理
//点击的默认操作也可以在这里处理
protected void onListItemClick(ListView l, View v, int position, long id)
{
Uri uri = ContentUris.withAppendedId(getIntent().getData(), id);
String action = getIntent().getAction();
if ( Intent.ACTION_EDIT.equals(action) )
{
//编辑联系人
startActivity(new Intent(Intent.ACTION_EDIT, uri));
}
else
{
//查看联系人
startActivity(new Intent(Intent.ACTION_VIEW, uri));
}
}
//长按界面触发的菜单
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo)
{
AdapterView.AdapterContextMenuInfo info;
try
{
info = (AdapterView.AdapterContextMenuInfo) menuInfo;
}
catch (ClassCastException e)
{
return;
}
//得到长按的数据项
Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
if (cursor == null)
{
return;
}
//设置上下文菜单的标题为日志标题
menu.setHeaderTitle(cursor.getString(1));
//添加删除菜单
menu.add(0, DELEContact_ID, 0, R.string.delete_user);
}
// 对于上下文菜单项选中的事件处理,是通过重载onContextItemSelected实现的。
@Override
public boolean onContextItemSelected(MenuItem item)
{
AdapterView.AdapterContextMenuInfo info;
try
{
info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
}
catch (ClassCastException e)
{
return false;
}
switch (item.getItemId())
{
case DELEContact_ID:
{
//删除一条记录
Uri noteUri = ContentUris.withAppendedId(getIntent().getData(), info.id);
getContentResolver().delete(noteUri, null, null);
return true;
}
}
return false;
}
}