Searching in the listview does not show the corresponding item

1

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

}
    
asked by anonymous 16.09.2017 / 04:06

1 answer

0

The reason for this is because you are using the position of the item clicked as index to get the song in the resID array.

The match between position and index is only correct when lstEstados_Encontrados has the same number of items as resID .

Suppose that when doing the search lstEstados_Encontrados it looks like this:

NHA
Rolezeira

When you click on Rolezeira the value obtained for the position is 1 . When you use this value in array resID you get the value corresponding to index 1, which is R.raw.eso .

Instead of having one array for the names and one for the resID create a class that contains these two values:

public class Content{

    private String nome;
    private int resID;

    public Content(String nome, int resID){

        this.nome = nome;
        this.resID = resID;
    }

    public String getNome(){
        return nome;
    }

    public int getResId(){
        return resID;
    }

    @Override
    public String toString(){
        return nome;
    }
}

Use this class as an item in the listContent and lstEstados_Encontrados :

private ArrayList<Content> listContent = new ArrayList<Content>();
private ArrayList<Content> lstEstados_Encontrados = new ArrayList<Content>();
    
16.09.2017 / 18:48