I am trying to create a gridView with ImageButtons inside them, so when pressed-a sound is emitted. So far I have my GridView_layout.xml, a layout for my button.xml. My gridAdapter to populate the grid with the buttons. With my current code the grid gets populated with the buttons, but when pressed no sound is emitted.
Here is part of my button activity which extends activity, the following code block is inside the onCreate() method:
final Context context = getApplicationContext();
final int duration = Toast.LENGTH_SHORT;
final ImageButton one = (ImageButton) findViewById(R.id.imageButton);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
one.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final MediaPlayer mp = MediaPlayer.create(getApplicationContext(), R.raw.pears);
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
if (mp == null) {
Toast toast = Toast.makeText(context, "button error", duration);
toast.show();
} else {
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaplayer) {
mp.stop();
mp.release();
}
});
mp.start();
}
}
});
}
});
getView() method from gridAdapter as requested:
public View getView(int position, View convertView, ViewGroup parent) {
ImageButton imageButton;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageButton = new ImageButton(mContext);
imageButton.setLayoutParams(new GridView.LayoutParams(190, 190));
imageButton.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageButton.setBackgroundColor(white);
} else {
imageButton = (ImageButton) convertView;
}
imageButton.setImageResource(mThumbIds[position]);
return imageButton;
}