我正在使用列表视图。我使用适配器为该列表添加项目。我正在尝试为列表的每个项目添加上下文菜单,但我对此有问题。
这是我的代码
项目清单:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/bgActivity"
android:orientation="horizontal" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="@color/textColorContact"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/pick_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="@drawable/app_icon"
android:text="@string/label_options" >
</Button>
</RelativeLayout>
名单 :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/lst_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@color/bgContactItem" />
</RelativeLayout>
菜单 :
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/CtxLblOpc1" android:title="OpcEtiqueta1"></item>
<item android:id="@+id/CtxLblOpc2" android:title="OpcEtiqueta2"></item>
</menu>
爪哇代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
// Getting reference to listview
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
registerForContextMenu(lstContacts);
}
@Override
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
}
/** Actions for the ContextMenu items */
@Override
public boolean onContextItemSelected(MenuItem item) {
//AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.CtxLblOpc1:
Toast.makeText(getApplicationContext(),
"Option 1",
Toast.LENGTH_LONG).show();
return true;
case R.id.CtxLblOpc2:
Toast.makeText(getApplicationContext(),
"Option 2",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
但我看不到上下文菜单,请帮忙,谢谢