在searchview中搜索,与项目不匹配

时间:2017-12-23 21:53:02

标签: java android listview audio adapter

我的代码如下工作,我有一个ArrayList音频,其各自的名称显示在ListView中,当点击播放音频时,到目前为止一切正常。

但是当我进行音频搜索并点击该项目时,它总是播放第一个声音,而不是应该发生的搜索声音,我认为我的适配器“memes.class”有问题,我是新手java / android,我不知道它是什么。

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

    }

    //here is where i do the searchview logic
    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();
    }
}

适配器

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

2 个答案:

答案 0 :(得分:0)

这是因为您从ArrayList获取值并且在ListView和ArrayList的搜索索引不相同之后。试试这段代码

public void playSong(int songIndex) {

    mp.reset();
    mp = MediaPlayer.create(this, lv.getAdapter().getItem(songIndex).getResId());

    mp.start();
}

答案 1 :(得分:0)

溶液

public void playSong(int songIndex) {

    mp.reset();
    mp = MediaPlayer.create(this, arrayAdapter.getItem(songIndex).getResId());

    mp.start();
}