Question

I have a simple app that is meant to add items to a list view when I click an add button on the options menu, and then later when I long click on one of the items added to the listview to then select either rename or remove and have that item in the listview either be renamed or removed. I can hard wire it to one of the items in the listview, but I can't figure out how to get which item was selected before the context menu comes up and then either rename or remove that selected one.

public class MainActivity_v3 extends ListActivity {

private int optionLastClickedId = -1;
private int optionClickedId = -1;
private boolean bSortByNum = true;

ArrayList<String> listItems=new ArrayList<String>();

ArrayAdapter<String> adapter;

AdapterContextMenuInfo info;

int internalPosition;

int clickCounter=0;

protected void onListItemClick(ListView l, View v, int position, long id){
    Toast toast = Toast.makeText(null, "you clicked " + position, 
Toast.LENGTH_LONG);
    toast.show();

}

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

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) 
menuInfo;
    internalPosition = info.position; 

    final MenuInflater inflater = getMenuInflater(); // why is this final
    if(v == getListView()){
        inflater.inflate(R.menu.list_menu_v3, menu);
    }else {
    inflater.inflate(R.menu.main_menu_v3, menu);
    }
}

public boolean onContextItemSelected(MenuItem item){

    if(item.getMenuInfo() != null && item.getMenuInfo() instanceof 
AdapterContextMenuInfo){
        info = (AdapterContextMenuInfo) item.getMenuInfo();

    }

    switch(item.getItemId()){
    case R.id.mi_delete:
        this.removeItems(internalPosition);

        break;
    case R.id.mi_rename:
        this.renameItems(internalPosition, "test1");

        break;
    case R.id.mi_cancel:
        break;
    }

    adapter.notifyDataSetInvalidated(); // test - remove later

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {

    int itemID = item.getItemId();

    switch(itemID){
    // switch(item.getItemId()){

            case R.id.add:
                this.addItems(getListView());
                break;

            case R.id.sort:
                Toast.makeText(this, "Sort", 
Toast.LENGTH_LONG).show();
                //adapter.sort();

    }

    return super.onOptionsItemSelected(item);

}

public boolean onCreateOptionsMenu(Menu menu) { 
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_menu_v3, menu);
    return true;
}

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main_v3);

    View textView = findViewById(R.id.text);
    registerForContextMenu(textView);
    registerForContextMenu(getListView());

    adapter=new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1,
        listItems);
    setListAdapter(adapter);


} // end - 0nCreate --------------------------------------

public void addItems(View v) {
    listItems.add("Clicked : "+clickCounter++);
    adapter.notifyDataSetChanged();
}

public void removeItems(int selectedItem){
    listItems.remove(selectedItem);
    //listItems.remove(v);
    adapter.notifyDataSetChanged();
}

public void renameItems(int index, String object){
    //listItems.set(v., object);
    listItems.set(index, object);
    adapter.notifyDataSetChanged();
}

public boolean onPrepareOptionsMenu(Menu menu){

    MenuItem item = menu.findItem(optionLastClickedId);

    if(item != null){
        item.setEnabled(true);
    }
    item = menu.findItem(optionClickedId);
    if(item != null){
        item.setEnabled(false);
    }
    optionLastClickedId = optionClickedId;

    return super.onPrepareOptionsMenu(menu);
}

}
Was it helpful?

Solution

Simply remove the data object from Adapter of your ListView. Which item to remove can be found from internalPosition. Check below code for how to find internalPosition.

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        final int internalPosition = info.position; 
   }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top