Search code examples
androidcustom-lists

How to set ImageView setOnClickListner which is in custom listview and play/pause song when Imageview clicked?


I am new to android and i want to play and pause song when ImageView clicked.According to my project I have one custom listview and in this listview i have two ImageView.So,when user select play at that time play the song and select pause at that time pause the song.

Note:- Basically I want to set that onClickListner in my activity class not to custom listview adapter.And i am adding that songs from res\raw folder to arraylist.

Here this my adapter

public class SongAdapter extends BaseAdapter{

Context context;
private ArrayList<SongModel> songsListDat = null;
public ArrayList<SongModel> songsList;
private static LayoutInflater inflater;

public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat) {

    this.context = activity;
    this.songsListDat = songsListDat;
    this.songsList = new ArrayList<SongModel>();
    this.songsList.addAll(songsListDat);
}
public void setSelectedIndex(int ind)
{
    notifyDataSetChanged();
}

@Override
public int getViewTypeCount() {

    return 1;
}

@Override
public int getCount() {
    return songsListDat.size();
}

@Override
public Object getItem(int position) {
    return songsListDat.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View vi = convertView;
    if (convertView == null)
        vi = inflater.inflate(R.layout.ringtone_row_layout, null);

    TextView title = (TextView) vi.findViewById(R.id.name); // title
    ImageView play = (ImageView)vi.findViewById(R.id.imgPlayPause);
    ImageView pause = (ImageView)vi.findViewById(R.id.imgPause);


    SongModel song = new SongModel();
    song = songsListDat.get(position);
    title.setText(song.getSongTitle());

    return vi;
}
}

This is activity class :- In this activity i want set onclicklistner for play and pause functionality.

public class MainActivity extends ListActivity {

public static final String TAG = "[MainActivity]";

Activity activity;
SongAdapter songAdapter;
SongsManager songsManager = new SongsManager();
MediaPlayer mp;

// Songs list
public ArrayList<SongModel> songsList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    activity = MainActivity.this;


    mp = new MediaPlayer();


    SongsManager plm = new SongsManager(activity);
    final ArrayList<SongModel> songsListData = songsManager.songList;


    this.songsList = plm.getAllSongs();


    for (int i = 0; i < songsListData.size(); i++) {
        SongModel song = songsListData.get(i);
        songsListData.add(song);
    }


    songAdapter = new SongAdapter(this, songsList);
    setListAdapter(songAdapter);

    ListView lv = getListView();
}
}

and this is my song model class

public class SongModel {

private String songPath, songTitle;

public SongModel(String songPath, String songTitle) {
    this.songPath = songPath;
    this.songTitle = songTitle;
}

public SongModel() {

}

public void setSongPath(String songPath){
    this.songPath = songPath;
}

public String getSongPath() {

    return songPath;
}
public void setSongTitle(String songTitle){
    this.songTitle = songTitle;
}

public String getSongTitle() {

    return songTitle;
}

}

If anyone know then please help me...


Solution

  • First Add a interface in your adapter class like & use it like this ::

    public class SongAdapter extends BaseAdapter {
    
        Context context;
        private ArrayList<SongModel> songsListDat = null;
        public ArrayList<SongModel> songsList;
        public OnMusicListener mListener;
        private static LayoutInflater inflater;
    
        public SongAdapter(Activity activity, ArrayList<SongModel> songsListDat, OnMusicListener mListener) {
    
            this.context = activity;
            this.mListener = mListener;
            this.songsListDat = songsListDat;
            this.songsList = new ArrayList<SongModel>();
            this.songsList.addAll(songsListDat);
        }
    
        public void setSelectedIndex(int ind) {
            notifyDataSetChanged();
        }
    
        @Override
        public int getViewTypeCount() {
    
            return 1;
        }
    
        @Override
        public int getCount() {
            return songsListDat.size();
        }
    
        @Override
        public Object getItem(int position) {
            return songsListDat.get(position);
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public int getItemViewType(int position) {
    
            return position;
        }
    
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
    
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View vi = convertView;
            if (convertView == null)
                vi = inflater.inflate(R.layout.ringtone_row_layout, null);
    
            TextView title = (TextView) vi.findViewById(R.id.name); // title
            ImageView play = (ImageView) vi.findViewById(R.id.imgPlayPause);
            ImageView pause = (ImageView) vi.findViewById(R.id.imgPause);
    
    
            SongModel song = new SongModel();
            song = songsListDat.get(position);
            title.setText(song.getSongTitle());
    
            play.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onPlayClicked(position);
                }
            });
            pause.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mListener.onPauseClicked(position);
                }
            });
    
            return vi;
        }
    
        public interface OnMusicListener {
            void onPauseClicked(int position);
    
            void onPlayClicked(int position);
        }
    }
    

    Then from your activity class

    songAdapter = new SongAdapter(this, songsList,this);
    setListAdapter(songAdapter);
    

    After "this " added you can now implements the method from interface

    from that you can have the click event of each play/pause of the list..

    Please tell if needed more help..

    So this will be your activity should like this ::

    public class MainActivity extends ListActivity implements SongAdapter.OnMusicListener{
    
        public static final String TAG = "[MainActivity]";
    
        Activity activity;
        SongAdapter songAdapter;
        SongsManager songsManager = new SongsManager();
        MediaPlayer mp;
    
        // Songs list
        public ArrayList<SongModel> songsList = new ArrayList<>();
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
    
            activity = MainActivity.this;
    
    
            mp = new MediaPlayer();
    
    
            SongsManager plm = new SongsManager(activity);
            final ArrayList<SongModel> songsListData = songsManager.songList;
    
    
            this.songsList = plm.getAllSongs();
    
    
            for (int i = 0; i < songsListData.size(); i++) {
                SongModel song = songsListData.get(i);
                songsListData.add(song);
            }
    
    
            songAdapter = new SongAdapter(this,songsList,this);
            setListAdapter(songAdapter);
    
            ListView lv = getListView();
    
        }
    
        @Override
        public void onPauseClicked(int position){
    
              //Todo-- Add code you want to perform on Pause clicked with   specific position
    
        }
        @Override
        public onPlayClicked(int position){
             //Todo-- Add code you want to perform on Play clicked with   specific position
        }
    }