2012-02-09 57 views
0

我有一個問題,顯示文件夾的媒體文件的列表視圖... 我試圖解決這個通過可擴展列表視圖(消耗品應該像上下文菜單行爲),但失敗非常...所以我決定通過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); 
     } 
    }*/ 



} 

回答

0

如果你想打開上下文菜單時,長按項目上完成,那麼你可以只註冊使用的初始化registerForContextMenu(查看)上下文菜單列表視圖。

如果ü要打開上下文菜單onItemClick然後u可以使用openContextMenu顯示上下文菜單。您還應該爲您的視圖註冊上下文菜單。

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

     } 
    } 



} 
相關問題