Skip to content Skip to sidebar Skip to footer

Android Delete File Using Context Menu

I've got a Listview showing files currently on the SDcard. When you long press the file, a context menu pops up. My question is: how do I pass in the selected item to the Context

Solution 1:

Well your answer is in your implementation itself. If you notice, in your onContextItemSelected() , the following statement brings in the info of the item you have selected in your main listview.

AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

You can use info.position to find out the position of your item in the list and then get the object from your ArrayList using songsList.get(info.position).

@OverridepublicbooleanonContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

    switch (item.getItemId()) {
    case R.id.delete: 
        Toast.makeText(this, "Delete Called.", Toast.LENGTH_SHORT).show();
        //Make sure songsList is a global variable so that it can be accessed here.HashMap<String, String> song = songsList.get(info.position);
        //Call your delete function to delete the song.returntrue;
    case R.id.share:
        Toast.makeText(this, "Share Called.", Toast.LENGTH_SHORT).show();

        default:
            returnsuper.onContextItemSelected(item);
    }
}

Solution 2:

Refer this LINK it passes varialble on long click.

below is the deletefile function

file.delete() will delete the file.

Post a Comment for "Android Delete File Using Context Menu"