如何将不同的声音添加到不同的列表项

时间:2015-06-16 11:51:04

标签: android

public class Dialog extends Activity {
    public MediaPlayer mp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        String[] dialouge ={"my name is ovi", "where do u live"};
        ListAdapter diaAadapter = new CustomAdapter(this, dialouge);
        ListView dia = (ListView) findViewById(R.id.dia);
        dia.setAdapter(diaAadapter);

        dia.setOnItemClickListener(
                new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                            int position, long id) {
                        String dialouge =String.valueOf(parent.getItemAtPosition(position));
                        MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.b);
                        mp.start();
                    }

                }
        );

    }
}

1 个答案:

答案 0 :(得分:0)

如果你想说点击不同的列表项目时如何播放不同的声音,那么如下所示:

dia.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        String dialouge =String.valueOf(parent.getItemAtPosition(position));

        switch(position){
               case 0:

                  MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.b);
                  mp.start();
                  break;

               case 1:
                  MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.a);
                  mp.start();
                  break;
               //and so on
        }
    }
});

您可以根据需要更改逻辑。