android使用上下文菜单删除文件
我有一个列表视图,显示SD卡上当前的文件。长按文件时,会弹出关联菜单 我的问题是:如何将所选项目传递到关联菜单,以便从列表中删除该文件,以及是否可以使用此选项将其从SD卡中删除?我的代码如下:android使用上下文菜单删除文件,android,contextmenu,delete-file,Android,Contextmenu,Delete File,我有一个列表视图,显示SD卡上当前的文件。长按文件时,会弹出关联菜单 我的问题是:如何将所选项目传递到关联菜单,以便从列表中删除该文件,以及是否可以使用此选项将其从SD卡中删除?我的代码如下: public class PlayListActivity extends ListActivity { // Songs list public ArrayList<HashMap<String, String>> songsList = new ArrayList<
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
}
公共类播放活动扩展了ListActivity{
//歌曲列表
public ArrayList songsList=new ArrayList();
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
ArrayList songsListData=新的ArrayList();
SongsManager plm=新的SongsManager();
//从SD卡获取所有歌曲
this.songsList=plm.getPlayList();
//循环播放列表
对于(int i=0;i
}请参考此项,它会在长时间单击后传递变量
下面是deletefile函数
file.delete()将删除该文件。答案在于实现本身。如果您注意到,在您的
onContextItemSelected()
,下面的语句将引入您在主listview中选择的项目的信息
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项。getMenuInfo()
您可以使用info.position查找项目在列表中的位置,然后使用songsList.get(info.position)从ArrayList获取对象
@覆盖
公共布尔值onContextItemSelected(MenuItem项){
AdapterContextMenuInfo信息=(AdapterContextMenuInfo)项。getMenuInfo();
开关(item.getItemId()){
案例R.id.delete:
Toast.makeText(这是“Delete Called.”,Toast.LENGTH_SHORT.show();
//确保songsList是一个全局变量,以便可以在此处访问它。
HashMap song=songsList.get(info.position);
//调用删除功能删除歌曲。
返回true;
案例R.id.share:
Toast.makeText(这是“共享名”),Toast.LENGTH_SHORT.show();
违约:
返回super.onContextItemSelected(项目);
}
}
好的,我有点理解这一点。那么这个对象就是“宋”?然后我如何使用它来删除或共享它?使用songsList.delete(歌曲)。应将其从歌曲列表中删除。删除(歌曲);似乎不起作用。它只是说在“歌曲列表”中添加演员阵容。知道我哪里出了问题吗?我通过选择currentSongIndex=info.position然后删除(currentSongIndex)使它正常工作;我的删除方法使用了songsList.get(songinindex.get)(“songPath”);然后检查文件是否存在,然后删除该文件。但是,现在我无法刷新列表,而不必返回到主活动,然后再次加载播放列表。关于如何刷新列表,有什么想法吗?我尝试查看notifyDataSetChanged,但这不起作用。你能详细说明一下吗?我将如何使用file.delete();是否与listview关联?
@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);
}
}