2017-08-22 2 views
0

J'ai trois activités à onglets chacune avec leurs propres images et tableaux de sons. Les images et les sons sont transmis au CustomListAdapter et retour en liste:Comment utiliser array pour Media player en utilisant CustomListAdapter pour différents onglets

TabActivity:

public class Tab1Activity extends Fragment { 

     MediaPlayer mp = new MediaPlayer(); 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.tab1, container, false); 

     String[] sound = {"path/to/media/file.mp3wl.gif", 
       "path/to/media/file.mp3", 
       "", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3", 
       "path/to/media/file.mp3" 
     }; 


     String[] images = {"ImagePathwl.gif", 
       "ImagePath/image.jpg", 
       "", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg", 
       "ImagePath/image.jpg" 
     }; 

     CustomListAdapter adapter=new CustomListAdapter(this.getActivity(), sound, images); 

     list=(ListView) rootView.findViewById(R.id.list_view); 
     list.setAdapter(adapter); 


     list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

       list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
            final int position, long id) { 
       // TODO Auto-generated method stub 


       try { 
        Thread t = new Thread(new Runnable() { 
         @Override 
         public void run() { 
          try { 
           mp.setDataSource(sound[+position]); 
           mp.prepare(); 
           mp.start(); 
          } catch (Exception e) { 
           e.printStackTrace(); 
           Log.e("GGGGGG", "prepare() failed"); 
          } 
         } 
        }); 
        t.start(); 

       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 

     mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 

      @Override 
      public void onCompletion(MediaPlayer mp) { 
       // TODO Auto-generated method stub 
       mp.reset(); 
      } 
     }); 


     return rootView; 

} 

CustomListAdapter:

public class CustomListAdapter extends ArrayAdapter<String> { 

private final String[] sound; 
private final String[] images; 

public CustomListAdapter(Activity context, String[] sound, images) { 
     super(context, R.layout.animal_list, itemname); 
     // TODO Auto-generated constructor stub 
     this.context=context; 
     this.sound=sound; 
     this.image=image; 
    } 

public View getView(int position,View view,ViewGroup parent) { 

LayoutInflater inflater=context.getLayoutInflater(); 
     View rowView=inflater.inflate(R.layout.animal_list, null,false); 

       imageView = (ImageView) rowView.findViewById(R.id.icon); 

       if (!imgid[position].isEmpty()) { 
      Picasso.with(context) // Context 
        .load(imgid[position]) // URL or file 
        .into(imageView); // An ImageView object to show the loaded image 
     } 

} 
} 

Comment jouer les sons avec le imageView dans customAdapter pour différents onglets ?

Répondre

0

J'ai déplacé la fonction de lecteur multimédia à l'adaptateur et dans un onclick là et transmis les sons de tableau à l'adaptateur.