【问题标题】:android delete file using context menuandroid使用上下文菜单删除文件
【发布时间】:2012-04-22 13:13:23
【问题描述】:

我有一个 Listview 显示 SD 卡上当前的文件。当你长按文件时,会弹出一个上下文菜单。

我的问题是:如何将所选项目传递给上下文菜单以从列表中删除文件,是否也可以使用它从 SD 卡中删除它?我的代码如下:

    public class PlayListActivity extends ListActivity {
// Songs list
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.playlist);

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

    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
    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<?> 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

}

}

【问题讨论】:

    标签: android contextmenu delete-file


    【解决方案1】:

    您的答案在于您的实现本身。如果您注意到,请在您的onContextItemSelected() ,以下语句会引入您在主列表视图中选择的项目的信息。

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

    您可以使用 info.position 找出您的项目在列表中的位置,然后使用 songList.get(info.position) 从您的 ArrayList 中获取对象。

    @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();
            //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.
    
            return true;
        case R.id.share:
            Toast.makeText(this, "Share Called.", Toast.LENGTH_SHORT).show();
    
            default:
                return super.onContextItemSelected(item);
        }
    }
    

    【讨论】:

    • 好的,我有点明白这一点。那么对象会是“歌曲”吗?然后我如何使用它来删除或共享它?
    • 使用songsList.delete(歌曲)。它应该从您的歌曲列表中删除
    • songsList.remove(song);似乎不起作用。它只是说将演员表添加到'songsList'。有什么想法我哪里出错了吗?
    • 我设法通过使用 currentSongIndex = info.position 然后 delete(currentSongIndex);我的删除方法使用了 songList.get(songIndex).get("songPath); 然后检查文件是否存在然后删除了该文件。但是,现在我无法刷新列表而无需返回主要活动然后加载再次播放列表。有关如何刷新列表的任何想法。我尝试查看 notifyDataSetChanged 但这不起作用。
    【解决方案2】:

    参考这个LINK 它在长按时通过变量。

    下面是deletefile函数

    file.delete() 将删除文件。

    【讨论】:

    • 你能详细说明一下吗?我将如何使用 file.delete();与列表视图有关?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-14
    • 2021-12-20
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    相关资源
    最近更新 更多