android:上下文菜单 +文件列表视图
我遇到了显示文件夹媒体文件的列表视图的问题...... 我尝试通过可扩展列表视图解决这个问题(消耗性项目应该像上下文菜单一样),但是失败了......所以我决定通过上下文菜单以简单的方式解决这个问题。
列表通过读取文件夹并过滤来获取其项目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);
}
}*/
}
I have got a problem with a listview that shows mediafiles of a folder...
I tried solving this via expandable listview (expendable items should behave like the context-menu), but failed terribly... so I decided to make it the simple way via contextmenu..
The list gets its items by reading a folder and filtering for mp3 & wav - files.
now, the contextmenu should have options "play", "stop" & "delete"
I figured out, how to make the file play via onListItemClick, but I don't quite get, how to put the options correctly in contextmenu and assign my list to it.
here is the code so far.
thanks for the help in advance.
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);
}
}*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
感谢您的帮助,终于弄清楚了。
然而,仍然有一些事情困扰着我..
使用删除时,文件被正确删除,但列表未更新。
另外 - 有什么方法可以定期生成列表吗?
thanks for the help, figured it out at last.
yet, there is still something bothering me..
when using delete the file is correctly deleted, yet the list not updated..
also - is there any way to generate the list periodically?
如果您想在长按项目后打开上下文菜单,那么您可以在初始化时使用 registerForContextMenu(View) 注册上下文菜单的列表视图。
如果您想在 ItemClick 上打开上下文菜单,则可以使用 openContextMenu 来显示上下文菜单。您还应该为您的视图注册上下文菜单。
If you want to open the Context Menu when a Long Click on Item is done then you can just register your listview for the Context Menu using registerForContextMenu(View) on Initialization.
And if u want to open Context Menu onItemClick then u can use openContextMenu to show the Context Menu. You should also register the Context Menu for your View.