Search in listview does not show the corresponding item

Asked

Viewed 184 times

1

In listview when searching for an item it shows the right matching 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;
}

}
  • This only happens when the list presents part of the songs or also when the list presents all of them?

  • @ramaral happens when the search is made, without search the audios play as they should

1 answer

0

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

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

Suppose when you search lstEstados_Encontrados gets like this:

NHA
Rolezeira

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

Instead of having an array for the names and another 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 a list item listContent and lstEstados_Encontrados:

private ArrayList<Content> listContent = new ArrayList<Content>();
private ArrayList<Content> lstEstados_Encontrados = new ArrayList<Content>();
  • What do I do in this case? some lines went red I think I did something wrong in the references http://prntscr.com/gm0ei6 print da sons.class http://prntscr.com/gm0gw2

  • something yes. I made the two references private ArrayList<sons> listContent = new ArrayList<sons>();&#xA;private ArrayList<sons> lstEstados_Encontrados = new ArrayList<sons>(); tried with content tbm

  • This "something" seems to me little, since I gave you all the information to be able to do everything. You have to fill in the Arraylist listContent with objects of the type Content: listContent.add(new Content("NHA", R.raw.ne));, do this for each of the sounds. Arrays listContentand resID are no longer required. You can change the name of the Content class but do not change its content. Change the rest of the code to stop using the above Array and switch to Arraylist. Don’t ask me to do all the code for you :)

  • you did as you asked, but I still have the msm problem, you could take a look at the code?

  • You must use the filtered Arraylist to get the music playing.

  • I got you already vlw

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.