2016-12-07 83 views
0

我有一個多頭列表inflatedcustom listview與按鈕。我想單獨播放單曲歌曲button。 我得到按鈕點擊的位置,並在播放歌曲方法中傳遞了歌曲。但它eventually它沒有播放任何歌曲。如果我設置playSong(1);那麼第一首歌曲正在播放。但是,如果我設置playSong(position);那麼就不會起到任何幫助song.Please播放歌曲從自定義Lisview在BuTTON點擊與位置

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(); 
     } 
    } 

回答

2

你可以嘗試從活動的setOnItemClickListner啓動您的播放器。

當您啓動您的活動時,將爲您的列表視圖的所有項目調用getView()方法。

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 
     } 
    }); 

我幫不了你更多,我不能看到的變量「MP」宣言和「songsList」

+0

thnku .. man !!!現在itz打 –

1

Adapter的責任是填充您的ListRecyclerView等,getView是綁定與單個項目。如果您ActivityFragment破壞你必須手動釋放MediaPlayer資源,這將頭很痛,你如此簡單,你應該通過增加onListItemClick播放歌曲的ActivityFragment

public void onListItemClick(ListView l, View v, int position, long id) { 

} 

現在你將有位置,播放和暫停你的歌曲。玩得開心:)

+0

thnku的建議! @Haris –