我有一个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
}
}
答案 0 :(得分:0)
答案 1 :(得分:0)
你的答案就在你的实施本身。如果您注意到,请在onContextItemSelected()
中
,以下语句将显示您在主列表视图中选择的项目的信息。
AdapterContextMenuInfo info =(AdapterContextMenuInfo)item.getMenuInfo();
您可以使用info.position查找列表中项目的位置,然后使用songsList.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);
}
}