Deleting items from ListView with a contextmenu in Android?

后端 未结 2 462
旧时难觅i
旧时难觅i 2021-01-07 10:42

I\'m trying to make a listmanager application in Android. I made a ListView, and an ArrayList into which I can add items with a button and an EditText. Then I made a context

相关标签:
2条回答
  • 2021-01-07 11:03

    I wrote above adapter code in my program, when i run program & delete item using context menu at that time force close window come

    this is my code

    public class ShowContextMenu extends ListActivity {

    ArrayAdapter<String> adapter;
    
    
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
       final String []s=(getResources().getStringArray(R.array.names));
    
       final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,s);
    
        setListAdapter(adapter);
    
    
        registerForContextMenu(getListView());
    }
    
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
    
    
    
    
    
    
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    
        String[] names = getResources().getStringArray(R.array.names);
    
        switch(item.getItemId()) {
    
        case R.id.edit:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.edit) + 
                    " context menu option for " + names[(int)info.id],                          
                    Toast.LENGTH_SHORT).show();
    
    
    
            return true;
    
        case R.id.save:
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.save) + 
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
    
        case R.id.delete:
    

    // insert code here

    adapter.remove(adapter.getItem(info.position));
    
    
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.delete) + 
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
    
            return true;
    
        case R.id.view:
    
            Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.view) + 
                    " context menu option for " + names[(int)info.id],
                    Toast.LENGTH_SHORT).show();
            return true;
        default:
            return super.onContextItemSelected(item);
        }
    }
    

    }

    0 讨论(0)
  • 2021-01-07 11:18

    This code should do the job:

    adapter.remove(adapter.getItem(info.position));
    
    0 讨论(0)
提交回复
热议问题