开发者

Android - ListView - ContextMenu doesn't shown on every list item

开发者 https://www.devze.com 2023-03-06 07:44 出处:网络
I\'ve got Main.java activity which owns ListView (simple Notepad app). I need context menu when click on listview item (single note) with \"copy\". I have 3 notes

I've got Main.java activity which owns ListView (simple Notepad app). I need context menu when click on listview item (single note) with "copy". I have 3 notes

  1. Long note
  2. URL note
  3. Short note

Only on URL note context menu is poping up when clicked. Why?

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

精彩评论

暂无评论...
验证码 换一张
取 消