从onContextItemSelected(Android)删除选定的列表视图文件

时间:2014-04-22 15:32:28

标签: android android-listview hashmap listadapter

我的活动实现了播放项目的列表视图,在ontext菜单上创建所选项目的longClick,这会浮动浮动删除"按钮"点击该按钮,应删除所选项目。问题是我不知道如何获取所选的文件名。

这是mainActivity:

    public class List2 extends ListActivity {
                ListView lv;
                ListAdapter adapter;
                String selection;


 public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
 ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.list2);
                        SongsManager plm = new SongsManager();
            // get all songs from sdcard
            this.songsList = plm.getPlayList();

            // looping through playlist
            for (int i = 0; i < songsList.size(); i++) {

                // creating new HashMap
                HashMap<String, String> song = songsList.get(i);

                // adding HashList to ArrayList
                songsListData.add(song);
            }

            // Adding menuItems to ListView
             adapter = new SimpleAdapter(this, songsListData,
           R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                            R.id.songTitle });



//         lv.invalidateViews();
            setListAdapter(adapter);
                    // selecting single ListView item
                  lv = getListView();

                 registerForContextMenu(getListView());


                    // listening to single listitem click
                    lv.setOnItemClickListener(new OnItemClickListener() {

                        @Override
            public void onItemClick(AdapterView<?> parent, View view,
                               final int position, long id) {
                            // getting listitem index
                       final  int songIndex = position;



    //                          // Starting new intent
                          Intent in = new Intent(getApplicationContext(),
                               AndroidBuildingMusicPlayerActivity.class);
                            // Sending songIndex to PlayerActivity
                            in.putExtra("songIndex", songIndex);

                            startActivity(in);
                        }
                    });

                  }





 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,View v,ContextMenuInfo menuInfo) { 
 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                    int songPos = info.position;

                    switch (item.getItemId()) {


                case R.id.delete:

        HashMap<String, String> song = songsList.get(info.position);


       final String SD_PATH = new String("/storage/extSdCard/MUSIC/song.mp3");

                    File file = new File (SD_PATH );
                    if (file.exists())
                    {

                    file.delete(); 

                    }  


                    return true;
                default:
                    return super.onContextItemSelected(item);
                }
             }



        }

此类从中获取方法getPlayList();

public class SongsManager {
    // SDCard Path
    final String MEDIA_PATH = new String("/storage/emulated/0/Music");


    private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


    public ArrayList<HashMap<String, String>> getPlayList(){
        File home = new File(MEDIA_PATH);

        if (home.listFiles(new FileExtensionFilter()).length > 0) {
            for (File file : home.listFiles(new FileExtensionFilter())) {
                HashMap<String, String> song = new HashMap<String, String>();
                song.put("songTitle", file.getName().substring(0, (file.getName().length() - 4)));
                song.put("songPath", file.getPath());

                // Adding each song to SongList
                songsList.add(song);
            }
        }
        // return songs list array
        return songsList;
    }

    /**
     * Class to filter files which are having .mp3 extension
     * */
    class FileExtensionFilter implements FilenameFilter {
        public boolean accept(File dir, String name) {
            return (name.endsWith(".mp3") || name.endsWith(".MP3"));
        }
    }

}

最后一个问题是如何在oncontextitemselected内永久删除所选列表视图文件(从选定路径)?

0 个答案:

没有答案