Search code examples
androidlistviewbaseadapter

Play song From Custom Listview On BuTTON CLICK with position


I have a list of longs which is inflated in custom listview with buttons. I want to play single song on single button click. I get the position of button click and also passed the song in play song method. But it eventually its not playing any of the song. If I set playSong(1); then the first song is playing. But if I set playSong(position); then it will not play any song.

 public abstract class CustomAdapter extends BaseAdapter implements Filterable , SeekBar.OnSeekBarChangeListener {

    Context context;
    ArrayList<String> countryList;
ArrayList<String> mStringFilterList;
    LayoutInflater inflter;
    public ImageView img2;
    Handler mHandler = new Handler();
    private SeekBar songProgressBar;

    public CustomAdapter(Context applicationContext, ArrayList<String> countryList) {
        this.context = applicationContext;
        this.countryList = countryList;
        mStringFilterList =  countryList;

        inflter = (LayoutInflater.from(applicationContext));
    }

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

    public void updateData(ArrayList<String> countryList) {
        this.countryList = countryList;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

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

    @Override
    public View getView(final int position, View view, ViewGroup viewGroup) {
        view = inflter.inflate(R.layout.list_itemss, null);
        String hello = countryList.get(position);
        Log.d("Hello",hello);
            playSong(1);
        String henno1 = String.valueOf(hello.length());
        Log.d("hellya",henno1);
        songProgressBar = (SeekBar) view.findViewById(R.id.songProgressBar);

        TextView country = (TextView) view.findViewById(R.id.textView);
        country.setText(hello);

        songProgressBar.setOnSeekBarChangeListener(this);

        songCurrentDurationLabel = (TextView) findViewById(R.id.songCurrentDurationLabel);
        songTotalDurationLabel = (TextView) findViewById(R.id.songTotalDurationLabel);


        img2 = (ImageView)view.findViewById(R.id.button3);
        img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Toast.makeText(Main.this,"Position is --"+position,Toast.LENGTH_LONG).show();
                int songIndex = position;

                playSong(songIndex);
                Log.d("song_index", String.valueOf(songIndex));

    /*I GET THE INDEX IN LOG*/

                if(mp.isPlaying()){
                    if(mp!=null){
                        mp.pause();
                        // Changing button image to play button
                        img2.setImageResource(R.drawable.playbutton);
                    }
                }else{
                    // Resume song
                    if(mp!=null){
                        mp.start();
                        // Changing button image to pause button
                        img2.setImageResource(R.drawable.pausebutton);
                    }
                }

                Toast.makeText(Main.this,"Play Song",Toast.LENGTH_LONG).show();
            }
        });

        ImageView img =(ImageView)view.findViewById(R.id.button);

        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(Main.this,"Added to Playlist",Toast.LENGTH_LONG).show();
                file = new File(path.get(position));
               customAdapter.notifyDataSetChanged();
            }
        });

        return view;
    }

    public void  playSong(int songIndex){
        // Play song
        Toast.makeText(Main.this,"Song is Playing",Toast.LENGTH_LONG).show();
        int m =songIndex;
//i ALSO GET THE POSITION IN LOG ON BUTTON CLICK BUT SONG IS NOT PLAYING.
        Log.d("Printsssss", String.valueOf(m));
        try {
            mp.start();
            mp.reset();
            mp.setDataSource(songsList.get(songIndex).get("songPath"));
            mp.prepare();
            mp.start();
            updateProgressBar();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Solution

  • You can try to launch your player from the setOnItemClickListner of your activity.

    When you launch your activity, the getView() method will be called for all items of your list view.

      listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                  //You have to put here the code to get your song from list of songs
            }
      });
    

    I can't help you more, i can't see declaration of variables "mp" and "songsList"