搜索播放列表并播放歌曲

时间:2014-05-09 04:24:55

标签: android listview search playlist

我正在尝试在音乐播放器中实现搜索。

 public class PlayListActivity extends ListActivity {
public ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
public String songList[];

    // Listview Adapter
    ArrayAdapter<String> adapter;
    // Search EditText
    EditText inputSearch;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.playlist);

    ArrayList<HashMap<String, String>> songsListData = new ArrayList<HashMap<String, String>>();

    SongsManager plm = new SongsManager(getApplicationContext());
    // get all songs
    this.songsList = plm.getPlayList();
    // looping through playlist
    for (int i = 0; i < songsList.size(); i++) {
        // creating new HashMap
        HashMap<String, String> song = songsList.get(i);
        // adding HashList to ArrayList
        songsListData.add(song);
    }

    inputSearch = (EditText) findViewById(R.id.inputSearch);

    // Adding menuItems to ListView
final ListAdapter adapter = new SimpleAdapter(this, songsListData,
R.layout.playlist_item, new String[] { "songTitle" }, new int[] {
                    R.id.songTitle });
    setListAdapter(adapter);

    // selecting single ListView item
    ListView lv = getListView();
    lv.setDivider(this.getResources().getDrawable(R.drawable.transperent_color));
    lv.setDividerHeight(1);
    // listening to single listitem click
    lv.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
    // getting listitem index
        int songIndex = position;

    // Starting new intent
   Intent in = new Intent(getApplicationContext(),
    MusicPlayerActivity.class);
        // Sending songIndex to PlayerActivity
        in.putExtra("songIndex", songIndex);
        setResult(100, in);
        // Closing PlayListView
        finish();
        }
    });

/**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            Log.v("LOG",cs.toString());
            ((Filterable) adapter).getFilter().filter(cs);  

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub                      
        }
    });

}
}

我可以搜索这首歌..但是如果我播放那首歌,则播放其他一些歌曲(根据位置编号) 如何更改它以便播放我搜索的特定歌曲..

感谢您的时间和帮助! :)

0 个答案:

没有答案