所以我有一个listView,它将显示来自外部存储的所有音频文件。我正在使用BaseAdapter,我可以用editText过滤它。问题是例如,如果名为“Argon”的音频文件位于第一位,如果我过滤listview并且名为“Camera”的音频文件出现在第一位,如果我点击相机,它仍然会播放氩。
OnItemClickListener:
songView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
music_column_index = musicCursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
musicCursor.moveToPosition(position);
String filename = musicCursor.getString(music_column_index);
Intent person = new Intent();
Bundle backpack = new Bundle();
backpack.putString("arnswer", filename);
person.putExtras(backpack);
setResult(RESULT_OK, person);
finish();
}
});
获取歌曲:
public void getSongList() {
ContentResolver musicResolver = getContentResolver();
Uri musicUri;
musicUri = android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
musicCursor = musicResolver.query(musicUri, null, null, null, null);
if(musicCursor!=null && musicCursor.moveToFirst()){
int titleColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media.TITLE);
int idColumn = musicCursor.getColumnIndex
(android.provider.MediaStore.Audio.Media._ID);
do {
long thisId = musicCursor.getLong(idColumn);
String thisTitle = musicCursor.getString(titleColumn);
songList.add(new Song(thisId, thisTitle));
}
while (musicCursor.moveToNext());
}
}
使用EditText过滤BaseAdapter:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
songView = (ListView)findViewById(R.id.PhoneMusicList);
songList = new ArrayList<Song>();
getSongList();
final SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
search = (EditText)findViewById(R.id.searchEt);
search.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
songAdt.getFilter().filter(s);
}
@Override
public void afterTextChanged(Editable s) {
}
});