Detecting which selected item (in a ListView) spawned the ContextMenu (Android)

后端 未结 8 793
难免孤独
难免孤独 2020-11-27 12:27

I have a ListView that will allow the user to long-press an item to get a context menu. The problem I\'m having is in determining which ListItem they long-press

相关标签:
8条回答
  • 2020-11-27 12:50

    We have used with success:

    @Override
    public boolean onContextItemSelected
    (
    MenuItem item
    )
        {
        if (!AdapterView.AdapterContextMenuInfo.class.isInstance (item.getMenuInfo ()))
            return false;
    
        AdapterView.AdapterContextMenuInfo cmi =
            (AdapterView.AdapterContextMenuInfo) item.getMenuInfo ();
    
        Object o = getListView ().getItemAtPosition (cmi.position);
    
        return true;
        }
    
    0 讨论(0)
  • 2020-11-27 12:52

    this is another way on how to create context menu n how to delete the item selected here is the whole code

         public class SimpleJokeList extends Activity {
    public static final int Upload = Menu.FIRST + 1;
    public static final int Delete = Menu.FIRST + 2;
    int position;
    ListView lv;
    EditText jokeBox;
    Button addJoke;
    MyAdapter adapter;
    private ArrayAdapter<String> mAdapter;
    private ArrayList<String> mStrings = new ArrayList<String>();
    String jokesToBeAdded;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.simplejokeui);
    
    
    
        lv=(ListView)findViewById(R.id.jokelist);
        addJoke=(Button)findViewById(R.id.addjoke);
        jokeBox=(EditText)findViewById(R.id.jokebox);
    
    
        mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
    
    
        registerForContextMenu(lv);
        listItemClicked();
        addJokes();
    
    private void addJokes() {
        // TODO Auto-generated method stub
        addJoke.setOnClickListener(new OnClickListener(){
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                jokesToBeAdded=jokeBox.getText().toString();
                if(jokesToBeAdded.equals("")){
                Toast.makeText(getApplicationContext(), "please enter some joke", Toast.LENGTH_LONG).show();
                }
                else{
                    lv.setAdapter(mAdapter);
                    mAdapter.add(jokesToBeAdded);
                    jokeBox.setText(null);
                }   
            }
        });
    }
    private void listItemClicked() {
        // TODO Auto-generated method stub
        lv.setOnItemLongClickListener(new OnItemLongClickListener() {
    
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                position=arg2;
                return false;
            }
        });
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        // TODO Auto-generated method stub
        super.onCreateContextMenu(menu, v, menuInfo);
        populateMenu(menu);
        menu.setHeaderTitle("Select what you wanna do");
    
    }
    private void populateMenu(ContextMenu menu) {
        // TODO Auto-generated method stub
         menu.add(Menu.NONE, Upload, Menu.NONE, "UPLOAD");
            menu.add(Menu.NONE, Delete, Menu.NONE, "DELETE");
    }
     @Override
        public boolean onContextItemSelected(MenuItem item) 
        {
         return (applyMenuChoice(item) || super.onContextItemSelected(item));
        }
    
    
    private boolean applyMenuChoice(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) 
        {   
             case Delete:
    
                 String s=mAdapter.getItem(position);
                 mAdapter.remove(s);
                // position--;
                 Toast.makeText(getApplicationContext(),"Congrats u HAve Deleted IT", Toast.LENGTH_LONG).show();
            return (true);
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题