Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid delete file using context menu
    primarykey
    data
    text
    <p>I've got a Listview showing files currently on the SDcard. When you long press the file, a context menu pops up.</p> <p>My question is: how do I pass in the selected item to the Context Menu in order to delete the file from the list, and is it possible to also delete it from the SDcard using this? My Code is as follows:</p> <pre><code> public class PlayListActivity extends ListActivity { // Songs list public ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.playlist); ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsListData = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;(); SongsManager plm = new SongsManager(); // get all songs from sdcard this.songsList = plm.getPlayList(); // looping through playlist for (int i = 0; i &lt; songsList.size(); i++) { // creating new HashMap HashMap&lt;String, String&gt; song = songsList.get(i); // adding HashList to ArrayList songsListData.add(song); } // Adding menuItems to ListView ListAdapter adapter = new SimpleAdapter(this, songsListData, R.layout.playlist_item, new String[] { "songTitle", "songDate" }, new int[] { R.id.songTitle, R.id.songDate }); setListAdapter(adapter); // setup ListView item ListView lv = getListView(); registerForContextMenu(lv); notifyDataSetChanged(); // listening to single listitem click lv.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) { // getting listitem index int songIndex = position; // Starting new intent Intent in = new Intent(getApplicationContext(), Bandboxstage.class); // Sending songIndex to PlayerActivity in.putExtra("songIndex", songIndex); setResult(100, in); // Closing PlayListView finish(); } }); } private void notifyDataSetChanged() { // TODO Auto-generated method stub } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { // TODO Auto-generated method stub super.onCreateContextMenu(menu, v, menuInfo); MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.context_menu, menu); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.delete: Toast.makeText(this, "Delete Called.", Toast.LENGTH_SHORT).show(); deleteFile(info.id); return true; case R.id.share: Toast.makeText(this, "Share Called.", Toast.LENGTH_SHORT).show(); default: return super.onContextItemSelected(item); } } private void deleteFile(long id) { // TODO Auto-generated method stub } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload