如何在listView中过滤歌曲

时间:2013-11-17 11:09:12

标签: android listview android-listview filter audio-player android-music-player

我有一个listView充满了sdcard上的所有歌曲,我可以使用ediText或其他东西(比如SearchBar)按名称过滤它们并更新listview吗?谢谢,这就是我填写列表视图的方法:

@SuppressWarnings("deprecation")
    private void init_phone_music_grid() {
            System.gc();
            String[] proj = { MediaStore.Audio.Media._ID,
    MediaStore.Audio.Media.DATA,
    MediaStore.Audio.Media.DISPLAY_NAME,
    MediaStore.Video.Media.SIZE };
            musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, null);
            count = musiccursor.getCount();
            musiclist = (ListView) findViewById(R.id.listView1);
            musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

            musiclist.setOnItemClickListener(musicgridlistener);

            mMediaPlayer = new MediaPlayer();


      }

  private OnItemClickListener musicgridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
    long id) {

                System.gc();
                  music_column_index = musiccursor
    .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                  musiccursor.moveToPosition(position);

                  String filename = musiccursor.getString(music_column_index);
                  String canzone=filename.replace("/storage/sdcard0/", "");
                  canzone=canzone.replace("Music/", "");
                  canzone=canzone.replace("media/", "");
                  canzone=canzone.replace("Ringtones/", "");
                  t=(TextView) findViewById(R.id.textView2);
                  t.setWidth(130);
                  t.setSingleLine(true);
                  t.setText(canzone);

                  try {
                        if (mMediaPlayer.isPlaying()) {
                              mMediaPlayer.reset();
                        }
                        mMediaPlayer.setDataSource(filename);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                  } catch (Exception e) {

                  }
            }
      };         
      public class MusicAdapter extends BaseAdapter {
            private Context mContext;

            public MusicAdapter(Context c) {
                  mContext = c;
            }

            public int getCount() {
                  return count;
            }

            public Object getItem(int position) {
                  return position;
            }

            public long getItemId(int position) {
                  return position;
            }

            public View getView(int position, View convertView, ViewGroup parent) {
                System.gc();
                String id = null;
                TextView tv;
                if (convertView == null) {
                tv = new TextView(mContext.getApplicationContext());
                } else{
                tv = (TextView) convertView;
                }
                musiccursor.moveToPosition(position);
                music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
                id = musiccursor.getString(music_column_index);
                tv.setText(id);
                return tv;
                }
      }

这是更新的代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_player);
    progressBar = (ProgressBar) findViewById(R.id.progressBar1);
    init_phone_music_grid(null);
    //Controllo();
    edittext=(EditText) findViewById(R.id.editText1);
    edittext.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {


            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                    int arg3) { }

            @Override
            public void afterTextChanged(Editable arg0) {
                init_phone_music_grid(edittext.getText().toString());

            }
        });
}

@SuppressWarnings("deprecation")
    private void init_phone_music_grid(String a) {
            System.gc();
            String sortOrder=null;
            if(a!=null){
                sortOrder=MediaStore.Audio.Media.TITLE +a;
            }

            String[] proj = { MediaStore.Audio.Media._ID,MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.DISPLAY_NAME,
    MediaStore.Video.Media.SIZE };
    musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, sortOrder);
            count = musiccursor.getCount();
            musiclist = (ListView) findViewById(R.id.listView1);
            musiclist.setAdapter(new MusicAdapter(getApplicationContext()));

            musiclist.setOnItemClickListener(musicgridlistener);

            mMediaPlayer = new MediaPlayer();


      }

      private OnItemClickListener musicgridlistener = new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position,
    long id) {

                System.gc();
                  music_column_index = musiccursor
    .getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
                  musiccursor.moveToPosition(position);

                  String filename = musiccursor.getString(music_column_index);
                  String canzone=filename.replace("/storage/sdcard0/", "");
                  canzone=canzone.replace("Music/", "");
                  canzone=canzone.replace("media/", "");
                  canzone=canzone.replace("Ringtones/", "");
                  t=(TextView) findViewById(R.id.textView2);
                  t.setWidth(130);
                  t.setSingleLine(true);
                  t.setText(canzone);

                  try {
                        if (mMediaPlayer.isPlaying()) {
                              mMediaPlayer.reset();
                        }
                        mMediaPlayer.setDataSource(filename);
                        mMediaPlayer.prepare();
                        mMediaPlayer.start();
                  } catch (Exception e) {

                  }


            }

      };

1 个答案:

答案 0 :(得分:1)

这是你从mediastore中排序东西的方式

String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";

如果你想通过艺术家或其他东西对它进行排序..只需用你需要的东西替换“TITLE”部分..

如果您觉得我的答案有用,请接受

编辑:对不起,我忘了你要把这个排序的东西放在哪里

musiccursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
    proj, MediaStore.Audio.Media.DURATION + ">= 120000", null, null);

在这一行节点中。用sortorder替换最后一个“null”,你将获得排序列表......