Проблема с пользовательским адаптером представления, который реализует кнопку формы слушателя и хронометр.
когда список заполняется в первый раз, все работает нормально (я могу нажать кнопку воспроизведения и увидеть, что хронометр работает нормально). но когда я прокручиваю до других элементов и возвращаюсь к первым элементам, прослушиватели кнопок не работают, а хронометр сбрасывается в 0 и прекращает прослушивание tickEvent.
Пожалуйста, используйте следующий фрагмент кода:
<!-- language: lang-java -->
public class PodcastListAdapter extends BaseAdapter implements OnPreparedListener, Runnable, OnChronometerTickListener{
MediaPlayer mediaPlayer;
Button start , stop , pause;
private List<Button> starts, stops, pauses;
TextView mobi ;
Chronometer chrono;
private List<Integer> usedPositions = new ArrayList<Integer>();
private static ProgressBar progressBar;
private List<Message> messageList;
private List<ProgressBar> progressBars;
private List<Chronometer> chronos;
private Context context;
public PodcastListAdapter(List<Message> tweetList, Context context) {
this.messageList = tweetList;
this.context = context;
this.progressBars=new LinkedList<ProgressBar>();
this.chronos=new LinkedList<Chronometer>();
this.starts=new LinkedList<Button>();
this.stops=new LinkedList<Button>();
this.pauses=new LinkedList<Button>();
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
myService.setPListener(this);
}
public int getCount() {
// TODO Auto-generated method stub
return messageList.size();
}
public Message getItem(int position) {
// TODO Auto-generated method stub
return messageList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//LinearLayout itemLayout;
RelativeLayout itemLayout;
//ProgressBar progressBar;
Log.d("debog", "onPrepared"+position);
final Message message = messageList.get(position);
itemLayout= (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.podcastmessage, parent, false);
TextView tvDate = (TextView) itemLayout.findViewById(R.id.PodcastDate);
tvDate.setText(message.getDate());
TextView tvCategory = (TextView) itemLayout.findViewById(R.id.PodcastCategory);
tvCategory.setText(message.getCategory());
//progress Bar
progressBar = (ProgressBar) itemLayout.findViewById(R.id.progressBar);
progressBar.setTag(position);
progressBars.add(position,progressBar);
//progressBar.setMax(100);
//progressBar.setProgress(50);
//progress Bar Fin
//récupérer le buttons
start=(Button) itemLayout.findViewById(R.id.play_pc);
start.setTag(position);
starts.add(position,start);
//associer les listners
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos=(Integer) v.getTag();
starts.get(pos).setEnabled(false);
stops.get(pos).setEnabled(true);
pauses.get(pos).setEnabled(true);
//start.setEnabled(false);
startStreamingAudio(messageList.get(pos).getLink().toString(),pos) ;
Log.d("StreamingAudio", "StartEvent");
Toast.makeText(context.getApplicationContext(), "Veuillez attendre le chargement... ", 3000).show();
}
});
stop=(Button)itemLayout.findViewById(R.id.stop_pc);
stop.setTag(position);
stops.add(position,stop);
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int pos=(Integer) v.getTag();
stopStreamingAudio(pos);
starts.get(pos).setEnabled(true);
stops.get(pos).setEnabled(false);
pauses.get(pos).setEnabled(false);
Log.d("StreamingAudio", "StopEvent");
}
});
pause=(Button)itemLayout.findViewById(R.id.pause_pc);
pause.setTag(position);
pauses.add(position,pause);
pause.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int pos=(Integer) v.getTag();
pauseStreamingAudio();
starts.get(pos).setEnabled(true);
pauses.get(pos).setEnabled(false);
stops.get(pos).setEnabled(true);
}
});
//chronometre
chrono=(Chronometer) itemLayout.findViewById(R.id.chrono);
chrono.setTag(position);
chrono.setOnChronometerTickListener(this);
chronos.add(position,chrono);
if(position%2==0)
{
//itemLayout.setBackgroundResource(R.drawable.cellule);
itemLayout.setBackgroundColor(Color.rgb(134, 137, 142));
//ImageView iv=(ImageView) itemLayout.findViewById(R.id.bg_cellule);
//iv.setVisibility(4);
tvCategory.setTextColor(Color.WHITE);
tvDate.setTextColor(Color.WHITE);
}
return itemLayout;
}
protected void pauseStreamingAudio() {
// TODO Auto-generated method stub
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
myService.pause();
}
private void startStreamingAudio(String link, int position) {
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
if(myService.isPaused())
{
myService.start();
chrono=chronos.get(position);
chrono.start();
return;
}
if(myService.isPlaying())
{
Log.d("startStream", "Stop");
myService.stop();
}
//préparation du lien (insérer %20 dans le nom du fichier)
link=link.replaceAll("\\s", "%20");
Log.d("ListAdapter", link);
myService.setSource(link);
//myService.start();
//myService.setProgressBar(progressBar);
progressBar=progressBars.get(position);
myService.setProgressBar(progressBar);
progressBar.setVisibility(ProgressBar.VISIBLE);
progressBar.setProgress(0);
progressBar.setMax(myService.getDuration());
//new Thread(this).start();//fo progress bar
//progressBar.setMax(100);
//progressBar.setProgress(50);
chrono=chronos.get(position);
//chrono.setBase(SystemClock.elapsedRealtime());
//chrono.start();
Log.d("duration", String.valueOf(myService.getDuration()));
}
private void stopStreamingAudio(int position) {
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
Log.d("stopStream","isplaying"+myService.isPlaying());
if(myService.isPlaying())
{
myService.stop();
//myService.getProgressBar().setProgress()
//chrono=chronos.get(position);
//chrono.stop();
}
else if(myService.isPaused())
{
myService.stop();
//myService.getProgressBar().setProgress()
//chrono=chronos.get(position);
//chrono.stop();
}
//chrono.setText("00:00");
}
public void run() {
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
int currentPosition= 0;
int total = myService.getDuration();
Log.d("run", "dans le thread"+total+myService.isPlaying());
while (myService.isPlaying()/* && currentPosition<total*/) {
try {
Thread.sleep(1000);
Log.d("run", "while dans le thread");
currentPosition= myService.getPosition();
Log.d("currentPosition", String.valueOf(currentPosition));
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
//progressBar=myService.getProgressBar();
//progressBar.setProgress(currentPosition);
if(myService.getProgressBar()!=null)
myService.getProgressBar().setProgress(currentPosition);
//Log.d("getProgress", String.valueOf(myService.getPosition()));
//Log.d("getDuration", String.valueOf(myService.getDuration()));
}
if(myService.getProgressBar()!=null)
myService.getProgressBar().setProgress(0);
}
public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
//Log.d("chrono", "chronotick");
//Log.d("chrono", ""+myService.getPosition());
if(myService.isPaused()) chronometer.stop();
else if(myService.isPlaying())
{
String m,s,currentTime;
long minutes=((myService.getPosition())/1000)/60;
if(minutes<10) m="0"+minutes;
else m=String.valueOf(minutes);
long seconds=((myService.getPosition())/1000)%60;
if(seconds<10) s="0"+seconds;
else s=String.valueOf(seconds);
currentTime=m+":"+s;
chronometer.setText(currentTime);
/*chronometer.setEms(myService.getPosition())*/;
}
else { chronometer.stop(); chronometer.setText("00:00");}
}
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
IPlayerService myService=(IPlayerService) RadioTab.getPlayerServiceConnection().getMyService();
myService.start();
new Thread(this).start();
chrono.start();
}
}