【发布时间】:2012-02-09 13:15:53
【问题描述】:
显示文件夹的媒体文件的列表视图有问题... 我尝试通过可扩展的列表视图来解决这个问题(可消耗的项目应该像上下文菜单一样),但是失败了......所以我决定通过上下文菜单让它变得简单......
列表通过读取文件夹并过滤 mp3 和 wav - 文件来获取其项目。 现在,上下文菜单应该有“播放”、“停止”和“删除”选项 我想出了如何通过 onListItemClick 播放文件,但我不太明白如何将选项正确地放在上下文菜单中并将我的列表分配给它。 这是到目前为止的代码。 提前感谢您的帮助。
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);
}
}*/
}
【问题讨论】:
标签: android android-listview contextmenu expandablelistview