我想在列表视图上放置一个上下文菜单。这似乎很简单,但我得到了一些有趣的行为。listview 有两个字段,一个 textview 和一个 editText。长按会打开 EditText 上的菜单,但不会在列表视图的其他地方打开。这是代码片段。
public class ManageClass extends ListActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.manageclass);
//Tons of not relevant stuff that I would be happy to provide.
pfdata = new PortfolioData(this);
try {
Cursor cursor = getClasses();
showClasses(cursor);
} finally {
pfdata.close();
}
}
private Cursor getClasses() {
String sql = "select c._id, c.NAME as NAME , c.percentage as PERCENTAGE "
+ "from asset_classes c;";
SQLiteDatabase db = pfdata.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
startManagingCursor(cursor);
return cursor;
}
private void showClasses(Cursor cursor) {
adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor,
FROM, TO, t);
// Log.d("TEST", Integer.toString(adapter.getCount()));
setListAdapter(adapter);
adapter.notifyDataSetChanged();
//I've tried moving this line around but it hasn't made a difference.
registerForContextMenu(getListView());
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, 0, "A");
menu.add(Menu.NONE, 1, 1, "B");
menu.add(Menu.NONE, 2, 2, "C");
}
最后,我承认的布局本身过于复杂
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/editText1"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="@string/TCLabel" />
<TextView
android:id="@+id/classtotalpercentage"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:textSize="50px" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/AddClassLabel"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:text="@string/AddClassLabel" />
<Button
android:id="@+id/addclass_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/addclassbutton_label" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:orientation="horizontal"
android:padding="10sp" >
<TextView
android:id="@+id/assetclassHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/assetclassHeader"
android:width="200dp" />
<TextView
android:id="@+id/percentageHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/assetclassHeader"
android:text="@string/percentageHeader"
android:width="100dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<TextView
android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_list_data" />
</RelativeLayout>
</LinearLayout>
我很感激任何建议。如果可能的话,我想要文本视图上的菜单而不是编辑文本。
更新。我发现它与 EditText 有关。如果我将其更改为 textview 或将其全部删除,那么一切都会正常运行。