In the listview when I search for an item it shows the right match name, but when I click to play it plays another item instead of the searched item.
MainActivity
public class MainActivity extends AppCompatActivity {
ListView lv;
MediaPlayer mp;
ArrayList<memes> item;
ArrayAdapter<memes> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.memes_main);
lv = findViewById(R.id.lv);
mp = new MediaPlayer();
item = new ArrayList<>();
item.add(new memes("Gemidão", R.raw.gemidaoremix));
item.add(new memes("Nunca nem vi", R.raw.nuncanemvi));
item.add(new memes("Caga", R.raw.caga));
item.add(new memes("Cagado de fome", R.raw.cagado));
item.add(new memes("Cala Boca", R.raw.calaboca));
item.add(new memes("Canal", R.raw.canal));
item.add(new memes("Capeta", R.raw.capeta));
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, item);
lv.setAdapter(arrayAdapter);
//play audio
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
playSong(position);
}
});
}
//aqui é onde faço a logica do searchview
public boolean onCreateOptionsMenu(final Menu menu){
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.busca, menu);
MenuItem menuItem = menu.findItem(R.id.sv);
SearchView searchView = (SearchView) menuItem.getActionView();
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
arrayAdapter.getFilter().filter(newText);
return true;
}
});
return super.onCreateOptionsMenu(menu);
}
public void playSong(int songIndex) {
mp.reset();
mp = MediaPlayer.create(this, item.get(songIndex).getResId());
mp.start();
}
@Override
public void onDestroy() {
super.onDestroy();
mp.release();
}
}
adapter
memes.class ##
public class memes{
private String nome;
private int resID;
memes(String nome, int resID){
this.nome = nome;
this.resID = resID;
}
public String getNome(){
return nome;
}
int getResId(){
return resID;
}
@Override
public String toString(){
return nome;
}
}