我有一個問題,顯示文件夾的媒體文件的列表視圖... 我試圖解決這個通過可擴展列表視圖(消耗品應該像上下文菜單行爲),但失敗非常...所以我決定通過contextmenu使它變成簡單的方式。android:上下文菜單+文件列表視圖
該列表通過閱讀文件夾並過濾mp3 & wav文件來獲取其項目。 現在,該文本菜單應該有選擇「播放」,「停止」 &「刪除」 我想通了,如何讓通過onListItemClick的文件播放,但我不完全得到,如何正確地把選項的ContextMenu和將我的列表分配給它。 這是迄今爲止的代碼。 感謝您的幫助提前。
import android.media.MediaPlayer;
import android.os.Bundle;
import android.widget.TextView;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
class Mp3WavFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return Pattern.matches(".*\\.(wav|mp3)", name);
}
}
public class TabAufnahmen extends ListActivity {
private static final String MEDIA_PATH = new String("/sdcard/Babyaufnahmen/");
private List<String> songs = new ArrayList<String>();
private MediaPlayer mp = new MediaPlayer();
TextView selection;
ListView list = (ListView)findViewById(R.id.list);
@Override
public void onCreate(Bundle icicle) {
try {
super.onCreate(icicle);
setContentView(R.layout.songlist);
updateSongList();
} catch (NullPointerException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
public void updateSongList() {
File home = new File(MEDIA_PATH);
if (home.listFiles(new Mp3WavFilter()).length > 0) {
for (File file : home.listFiles(new Mp3WavFilter())) {
songs.add(file.getName());
}
/*ArrayAdapter<String> songList = new ArrayAdapter<String>(this,R.layout.song_item, songs);
list.setAdapter(songList);
//list.setListAdapter(songList);
registerForContextMenu(list);*/
/* setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, songs));
selection=(TextView)findViewById(R.id.selection);
registerForContextMenu(getListView());*/
}
}
/* @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
try {
mp.reset();
mp.setDataSource(MEDIA_PATH + songs.get(position));
mp.prepare();
mp.start();
} catch(IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}*/
/*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) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
String[] names = getResources().getStringArray(R.array.names);
switch(item.getItemId()) {
case R.id.abspielen:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.abspielen) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
case R.id.anhalten:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.anhalten) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
case R.id.loeschen:
Toast.makeText(this, "You have chosen the " + getResources().getString(R.string.loeschen) +
" context menu option for " + names[(int)info.id],
Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}*/
}