Skip to content
Advertisement

Android how to play different sound for each item in recyclerview

I build app with images of animals, and after you click on animal, its play sound for that animal.

I use Recyclerview for displaying all animals items.

My problem is that i do not know how to handle click event for playing sounds for each animal. I do know how to play same sound for each item in recyclerview but i do not know how to handle specific sound for each item.

this is code for my class where i handle data, where i put all animal names, images and sounds for each object.

package com.example.android.animalsoundsforchildren;

import java.util.ArrayList;


public class Zivotinje {
    private String mAnimal_name;
    private int mAnimal_slika;
    private int mSound;


    public Zivotinje(String animalName, int slika, int sound) {
        this.setmAnimal_name(animalName);
        this.setmAnimal_slika(slika);
        this.setmSound(sound);
    }

    public String getmAnimal_name() {
        return mAnimal_name;
    }

    public void setmAnimal_name(String mAnimal_name) {
        this.mAnimal_name = mAnimal_name;
    }

    public int getmAnimal_slika() {
        return mAnimal_slika;
    }

    public void setmAnimal_slika(int mAnimal_slika) {
        this.mAnimal_slika = mAnimal_slika;
    }

    public int getmSound() {
        return mSound;
    }

    public void setmSound(int mSound) {
        this.mSound = mSound;
    }

    //putting data
    public static ArrayList<Zivotinje> createZivotinjeList() {
        ArrayList<Zivotinje> animals = new ArrayList<Zivotinje>();

        animals.add(new Zivotinje("Sheep", R.drawable.domestic_sheep, R.raw.gun));
        animals.add(new Zivotinje("Sparrow", R.drawable.birds_sparrow, R.raw.birds_sparrow));
        animals.add(new Zivotinje("Dolphin", R.drawable.sea_dolphin, R.raw.dolphin));
        animals.add(new Zivotinje("Bumblebee", R.drawable.insects_bumblebee, R.raw.bumblebee));
        animals.add(new Zivotinje("Tractor", R.drawable.cars_tractor, R.raw.tractor));
        animals.add(new Zivotinje("Baby Cry", R.drawable.laugh_baby_cry, R.raw.baby_cray));
        animals.add(new Zivotinje("Wind", R.drawable.nature_wind, R.raw.wind));
        animals.add(new Zivotinje("Golf", R.drawable.effects_golf, R.raw.golf));
        animals.add(new Zivotinje("Clock", R.drawable.others_clock, R.raw.clock));


        return animals;
    }
}

This is code for Adapter, and here i handle click and when i click on item, it play the same sound but how to play different sound for each animal, not the same sound like here. Here i always play sound R.raw.clock

package com.example.android.animalsoundsforchildren;

import android.content.Context;
import android.media.MediaPlayer;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

    @Override
    public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {

        Zivotinje zivotinjeIme = mZivotinje.get(position);

        // Set item views based on the data model
        TextView textView = viewHolder.nameTextView;
        textView.setText(zivotinjeIme.getmAnimal_name());

        ImageView slika_source = viewHolder.imageView;
        slika_source.setImageResource(zivotinjeIme.getmAnimal_slika());





    }


    @Override
    public int getItemCount() {
        return mZivotinje.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView nameTextView;




        public ViewHolder(final View itemView) {
            super(itemView);

            nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
            imageView = (ImageView) itemView.findViewById(R.id.slika_id);





            //It works when i play for each item sound but how to play specific sound for each animal
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), R.raw.clock);
                    mediaPlayer.start();
                }
            });

        }
    }

    private List<Zivotinje> mZivotinje;

    public ZivotinjeAdapter(List<Zivotinje> animals) {
        mZivotinje = animals;
    }
}

Advertisement

Answer

Refer the link https://gist.github.com/grantland/cd70814fe4ac369e3e92

you can refer to the item click event and how to get the item on click event

please refer the code below. i have made the changes

public class ZivotinjeAdapter extends RecyclerView.Adapter<ZivotinjeAdapter.ViewHolder> {

    @Override
    public ZivotinjeAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        Context context = parent.getContext();
        LayoutInflater inflater = LayoutInflater.from(context);

        // Inflate the custom layout
        View contactView = inflater.inflate(R.layout.item_zivotinje, parent, false);

        // Return a new holder instance
        ViewHolder viewHolder = new ViewHolder(contactView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(ZivotinjeAdapter.ViewHolder viewHolder, int position) {

        Zivotinje zivotinjeIme = mZivotinje.get(position);

        // Set item views based on the data model
        TextView textView = viewHolder.nameTextView;
        textView.setText(zivotinjeIme.getmAnimal_name());

        ImageView slika_source = viewHolder.imageView;
        slika_source.setImageResource(zivotinjeIme.getmAnimal_slika());

       viewHolder.setItem(mZivotinje[position]);




    }


    @Override
    public int getItemCount() {
        return mZivotinje.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public ImageView imageView;
        public TextView nameTextView;
        private Zivotinje mItem;





        public ViewHolder(final View itemView) {
            super(itemView);

            nameTextView = (TextView) itemView.findViewById(R.id.zivotinjaIme_id);
            imageView = (ImageView) itemView.findViewById(R.id.slika_id);


 public void setItem(Zivotinje item) {
            mItem = item;

        }




            //It works when i play for each item sound but how to play specific sound for each animal
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                      // here you will get mItem and from that you can get the sound
                    MediaPlayer mediaPlayer = MediaPlayer.create(v.getContext(), R.raw.clock);
                    mediaPlayer.start();
                }
            });

        }
    }

    private List<Zivotinje> mZivotinje;

    public ZivotinjeAdapter(List<Zivotinje> animals) {
        mZivotinje = animals;
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement