1

我有ListView这是在Fragment。我一直在试图添加一个ContextMenu来编辑和删除列表中的项目。列表项是可点击的,但没有菜单出现。我尝试加入if (getUserVisibleHint()) {但未成功。预先感谢您的意见。片段上下文菜单中的ListView

public static class FragmentS extends Fragment { 

    private ListView saveListView; 
    private List<LiftSave> LiftSaves = new ArrayList<LiftSave>(); 
    private static final int EDIT = 0, DELETE = 1; 

    int longClickedItemIndex; 
    DatabaseHandler dbHandler; 
    ArrayAdapter<LiftSave> saveAdapter; 

    public FragmentS() { 

    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.listview_s, 
       container, false); 
     saveListView = (ListView) rootView.findViewById(R.id.saveListView); 

     DatabaseHandler dbHandler; 
     dbHandler = new DatabaseHandler (getActivity().getApplicationContext()); 
     if (dbHandler.getLiftSavesCount() != 0) 
      LiftSaves.addAll(dbHandler.getAllLiftSaves()); 

     populateList(); 

     saveListView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { 
      @Override 
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { 
       longClickedItemIndex = position; 
       return false; 
      } 
     }); 
     return rootView; 
    } 

    private void populateList() { 
     ArrayAdapter<LiftSave> saveAdapter = new SaveListAdapter(); 
     saveListView.setAdapter(saveAdapter); 
    } 

    public class SaveListAdapter extends ArrayAdapter<LiftSave> { 
      public SaveListAdapter() { 
       super(getActivity(), R.layout.listview_item, LiftSaves); 
      } 

      @Override 
      public View getView(int position, View view, ViewGroup parent) { 
       if (view == null) 
        view = getActivity().getLayoutInflater().inflate(R.layout.listview_item, parent, false); 

       LiftSave currentLiftSave = LiftSaves.get(position); 

       TextView liftName = (TextView) view.findViewById(R.id.liftName); 
       liftName.setText(currentLiftSave.getLiftName()); 
       TextView maxValue = (TextView) view.findViewById(R.id.maxValue); 
       maxValue.setText(currentLiftSave.getMaxValue()); 
       TextView weightAndReps = (TextView) view.findViewById(R.id.weightAndReps); 
       weightAndReps.setText(currentLiftSave.getRepsAndWeight()); 
       TextView liftNotes = (TextView) view.findViewById(R.id.liftNotes); 
       liftNotes.setText(currentLiftSave.getLiftNotes()); 
       TextView date = (TextView) view.findViewById(R.id.todayDate); 
       date.setText(currentLiftSave.getTodayDate()); 

       return view; 
      } 

    } 
    public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) { 
     super.onCreateContextMenu(menu, view, menuInfo); 

     menu.setHeaderIcon(R.drawable.pencil_icon); 
     menu.setHeaderTitle("Save Options"); 
     menu.add(Menu.NONE, EDIT, menu.NONE, "Edit Save"); 
     menu.add(Menu.NONE, DELETE, menu.NONE, "Delete Save"); 
    } 

    public boolean onContextItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case EDIT: 
       // TODO: Add save edit code 
       break; 
      case DELETE: 
       dbHandler.deleteLiftSave(LiftSaves.get(longClickedItemIndex)); 
       LiftSaves.remove(longClickedItemIndex); 
       saveAdapter.notifyDataSetChanged(); 
       break; 
     } 

     return super.onContextItemSelected(item); 
    } 
} 

XML:

<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" 
tools:context=".TabbedActivity$FragmentS" 
android:background="@android:color/holo_blue_dark"> 

<LinearLayout 
android:id="@+id/tabSaveList" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Saved Maxes" 
    android:id="@+id/textView" 
    android:layout_gravity="center" 
    android:layout_marginTop="10dp" 
    android:textColor="#fffaf4a1" 
    android:textStyle="bold" /> 

    <ListView 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/saveListView" /> 


    </LinearLayout> 
    </RelativeLayout> 
+0

您是否将此片段添加到活动中?因为**“一个片段不能在屏幕上获得视图,只有当它被放置在一个活动的层次结构中时,它的视图才会出现”** – li2 2015-04-06 01:39:00

+0

@ li2感谢您的回复。我会研究这一点,但到目前为止,我还没有遇到过这样的问题。我的MainActivity扩展了FragmentActivity。我在下面提到的@Thom Palmer中缺少'registerForContextMenu'行。它现在工作正常,菜单显示。 – Austin 2015-04-06 02:06:31

回答

1

在你onCreateView,你需要通过添加registerForContextMenu(saveListView)对上下文菜单关联到列表视图。有关更多信息,请参阅docs

+0

哎呦。就是这样。我必须在某个时候意外切断它。谢谢! – Austin 2015-04-06 02:03:23