如何设置我的代码以点击网格视图中的第一项? 我得到了这个`
mp=MediaPlayer.create(this, R.raw.snd1);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mp.start();
switch (position) {
case 0:
break;
case 1:
break;
}
}
}); `
我得到了这段代码,但它正在网格视图中的每个项目上播放声音。 如何设置它以便仅在第一次点击?还是第二或三维?
答案 0 :(得分:3)
只需改为:
mp=MediaPlayer.create(this, R.raw.snd1);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
mp.start(); // This will make sound
break;
case 1:
mp2.start(); // This will make different sound
break;
}
}
});
答案 1 :(得分:2)
你不必把你的mp.start();
放到开关上吗?像那样:
mp=MediaPlayer.create(this, R.raw.snd1);
gridView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
mp.start();
case 1:
break;
}
}
});