Android - ListView - ContextMenu没有显示在每个列表项上

时间:2011-05-17 15:52:24

标签: android listview contextmenu

我有Main.java活动,它拥有ListView(简单的记事本应用程序)。点击listview项目(单注)和“复制”时,我需要上下文菜单。我有3个笔记

  1. 长记
  2. 网址备注
  3. 简短说明
  4. 单击时,仅在URL注释上下文菜单中弹出。为什么呢?

    public class Main extends Activity {
        private Button createButton;
        private ListView notesListView;
        private ArrayList<Note> notes;
        private Note selectedNote;
        public static final int noteEditorRequest = 1;
        public static final int noteCreateReqeust = 2;
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            notes = NoteFileAdapter.getNotes();
            selectedNote = null;
    
            if (notes == null) {
                Toast.makeText(this, R.string.err_read, Toast.LENGTH_LONG).show();
                notes = new ArrayList<Note>();
            }
    
            createButton = (Button) findViewById(R.id.mainCreateButton);
            createButton.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Main.this, NoteEditor.class);
                    intent.putExtra("request", noteCreateReqeust);
                    startActivityForResult(intent, noteCreateReqeust);
                }
            });
    
            notesListView = (ListView) findViewById(R.id.mainNotesListView);
            notesListView.setAdapter(new MainNoteAdapter(this, notes));
            registerForContextMenu(notesListView);
        }
    
        @Override
        public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {       
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
            selectedNote = (Note) notesListView.getItemAtPosition(info.position);
    
            menu.setHeaderTitle(R.string.contextmenu_title);
            menu.add(R.string.contextmenu_copy).setOnMenuItemClickListener(
                    new OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
                            clipboardManager.setText(selectedNote.getTextContent());
                            return true;
                        }
                    });
        }
    

0 个答案:

没有答案