2016-08-02 20 views
0

私はlistview(カスタムアダプター)の問題に直面しています。オーディオを再生するメディアプレーヤー(mp3ファイル)でリストビューを実装しました。私はこれがリサイクルビューであることを知っています。私の問題は、6-7行の同じmp3ファイルが0番目の位置にあったファイルと同じに再生された後です。 これに対する解決策はありますか? 私の主な活動 パブリッククラスMainActivityアクティビティを拡張することがconvertViewが再利用になりという理由だけだ{listview(カスタムアダプター)with media player android

ListView lview3; 
    ListViewCustomAdapter adapter; 
    private ArrayList<Object> itemList; 
    private ItemBean bean; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     prepareArrayLits(); 
     lview3 = (ListView) findViewById(R.id.listView1); 
     adapter = new ListViewCustomAdapter(this, itemList); 
     lview3.setAdapter(adapter); 

     lview3.setOnItemClickListener(this); 
    } 

    @Override 
    public void onItemClick(AdapterView<?> arg0, View arg1, int position1, long id) { 
     // TODO Auto-generated method stub 
     ItemBean bean = (ItemBean) adapter.getItem(position1); 

     Toast.makeText(this, "Title => "+bean.getTitle()+" \n Description => "+bean.getDescription(), Toast.LENGTH_SHORT).show(); 
    } 

    /* Method used to prepare the ArrayList, 
    * Same way, you can also do looping and adding object into the ArrayList. 
    */ 
    public void prepareArrayLits() 
    { 
     itemList = new ArrayList<Object>(); 

     AddObjectToList(R.drawable.ic_add, "Add", "Add desc",R.raw.mp1); 
     AddObjectToList(R.drawable.ic_delete, "Delete", "Delete desc",R.raw.mp2); 
     AddObjectToList(R.drawable.ic_down, "Down", "Down desc",R.raw.mp3); 
     AddObjectToList(R.drawable.ic_info, "Information", "Information desc",R.raw.mp4); 
     AddObjectToList(R.drawable.ic_help, "Help", "Help desc",R.raw.mp5); 
     AddObjectToList(R.drawable.ic_download, "Download", "Download desc",R.raw.mp6); 
     AddObjectToList(R.drawable.ic_mail, "Mail", "Mail desc",R.raw.mp7); 
     AddObjectToList(R.drawable.ic_search, "Search", "Search desc",R.raw.mp8); 
     AddObjectToList(R.drawable.ic_settings, "Settings", "Settings desc",R.raw.mp9); 
    } 

    // Add one item into the Array List 
    public void AddObjectToList(int image, String title, String desc,int audio) 
    { 
     bean = new ItemBean(); 
     bean.setDescription(desc); 
     bean.setImage(image); 
     bean.setTitle(title); 
     bean.setAudio(audio); 
     itemList.add(bean); 
    } 
} 

私listviewCustomadapter

public class ListViewCustomAdapter extends ArrayAdapter { 
    MediaPlayer mPlayer; 
    Context c; 
    ArrayList<Object> itemList; 

    public Activity context; 
    public LayoutInflater inflater; 

    public ListViewCustomAdapter(Activity context, ArrayList<Object> itemList) { 
     super(context, R.layout.main); 

     this.context = context; 
     this.itemList = itemList; 
     this.c = context; 
     this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    } 

    @Override 
    public int getCount() { 
     // TODO Auto-generated method stub 
     return itemList.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return itemList.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return 0; 
    } 

    public static class ViewHolder { 
     ImageView imgViewLogo; 
     TextView txtViewTitle; 
     TextView txtViewDescription; 
     Button btn1, btn2; 
    } 

    @Override 
    public View getView(final int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 

     ViewHolder holder; 
     if (convertView == null) { 
      holder = new ViewHolder(); 
      convertView = inflater.inflate(R.layout.items, null); 

      holder.imgViewLogo = (ImageView) convertView.findViewById(R.id.imgViewLogo); 
      holder.btn1 = (Button) convertView.findViewById(R.id.button); 
      holder.btn1.setOnClickListener(new View.OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        if (position == 0) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp1); 
         mPlayer.start(); 
        } 
        if (position == 1) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp2); 
         mPlayer.start(); 
        } 
        if (position == 2) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp3); 
         mPlayer.start(); 
        } 
        if (position == 3) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp4); 
         mPlayer.start(); 
        } 
        if (position == 4) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp5); 
         mPlayer.start(); 
        } 
        if (position == 5) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp6); 
         mPlayer.start(); 
        } 
        if (position == 6) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp7); 
         mPlayer.start(); 
        } 
        if (position == 7) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp8); 
         mPlayer.start(); 
        } 
        if (position == 8) { 
         mPlayer = MediaPlayer.create(getContext(), R.raw.mp9); 
         mPlayer.start(); 
        } 


       } 
      }); 


      holder.btn2 = (Button) convertView.findViewById(R.id.button2); 
      holder.txtViewTitle = (TextView) convertView.findViewById(R.id.txtViewTitle); 
      holder.txtViewDescription = (TextView) convertView.findViewById(R.id.txtViewDescription); 

      convertView.setTag(holder); 
     } else 
      holder = (ViewHolder) convertView.getTag(); 

     ItemBean bean = (ItemBean) itemList.get(position); 

     holder.imgViewLogo.setImageResource(bean.getImage()); 
     holder.txtViewTitle.setText(bean.getTitle()); 
     holder.txtViewDescription.setText(bean.getDescription()); 

     return convertView; 
    } 

} 

答えて

0

OnItemClickListener実装します。 (convertView != null)でもholder.btn1.setOnClickListener(new View.OnClickListener() { ...に設定してください。

また、すべてのビューアイテムに対して、MediaPlayerを何度も繰り返し作成するよりも、それを使用する方がよい場合があります。

+0

ありがとうmahn u rock :) – Ezio

関連する問題