0

我有一个 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

}

}

4

2 回答 2

0

请参阅此链接,它在长按时通过变量。

下面是deletefile函数

file.delete() 将删除文件。

于 2012-04-22T04:57:55.217 回答
0

那么你的答案就在你的实现本身中。如果您注意到,在您的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);
    }
}
于 2012-04-22T05:13:23.353 回答