android:上下文菜单+ listview文件

时间:2012-02-09 13:15:54

标签: android android-listview contextmenu expandablelistview

我的listview显示文件夹的媒体文件有问题... 我尝试通过可扩展的listview来解决这个问题(可消耗的项目应该像上下文菜单一样),但是非常失败...所以我决定通过contextmenu将它变成简单的方法..

该列表通过读取文件夹并过滤mp3&而获取其项目。 wav - 文件。 现在,contextmenu应该有“play”,“stop”和& “删除” 我想通过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);
        }
    }*/



}

2 个答案:

答案 0 :(得分:1)

感谢你的帮助,终于明白了。 然而,还有一些困扰我的事情...... 使用删除时,文件被正确删除,但列表没有更新.. 还 - 有没有办法定期生成列表?

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();

    @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);
            this.setListAdapter(songList);
            registerForContextMenu(getListView());
        }       
    }
    //der player
    @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.);
        switch(item.getItemId()) {
        case R.id.abspielen:
            Toast.makeText(this, "Sie haben " + getResources().getString(R.string.abspielen) + "für die Datei" + getListView().getAdapter().getItem(info.position).toString() + " gewählt",
                    Toast.LENGTH_LONG).show();
            try {

                mp.reset();
                mp.setDataSource(MEDIA_PATH + getListView().getAdapter().getItem(info.position).toString());
                mp.prepare();
                mp.start();
            } catch(IOException e) {
                Log.v(getString(R.string.app_name), e.getMessage());
            } 

            return true;
        case R.id.loeschen:
            Toast.makeText(this, "Sie haben " + getResources().getString(R.string.loeschen) + "für die Datei" + getListView().getAdapter().getItem(info.position).toString() + " gewählt",
                    Toast.LENGTH_SHORT).show();
            File file = new File("/sdcard/Babyaufnahmen/" + getListView().getAdapter().getItem(info.position).toString());
            boolean deleted = file.delete();


            return true;
        default:
            return super.onContextItemSelected(item);

        }
    }



}

答案 1 :(得分:0)

如果要在完成长项单击后打开上下文菜单,则可以使用初始化时的registerForContextMenu(View)注册上下文菜单的列表视图。

如果你想在onItemClick上打开Context Menu,那么你可以使用openContextMenu来显示上下文菜单。您还应该为视图注册上下文菜单。