Reputation: 253
A problem with a custom view adapter that implement listeners form button and chronometer. when the list is populated for the first time, everything works fine(i can click the play button and see the chronometer working fine). but when i scroll to the other items and return to the first elements the button listeners won't work and the chronometer is reset to 0 and stop listening to the tickEvent.
Please refer the following code snippet :
<!-- 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();
}
}
Upvotes: 0
Views: 1967
Reputation: 2320
you can perform this below way
package com.CommonMethod.Activity;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
public class ListViewDemo2 extends Activity {
ListView LS;
LayoutInflater Linflater;
public static LinearLayout LL;
public static TextView txtOne;
public static TextView txtTwo;
public static Button btnone;
public static Button btnTwo;
public static Button btnThree;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listviewdemo);
LS = (ListView) findViewById(R.id.LSOne);
Linflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LS.setAdapter(new ListAdapter(this));
}
public class ListAdapter extends BaseAdapter {
public ListAdapter(Context con) {
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getViewTypeCount() {
// TODO Auto-generated method stub
return 10;
}
@Override
public int getItemViewType(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View ReturnView = convertView;
int _intPostion = getItemViewType(position);
if (ReturnView == null) {
ReturnView = Linflater.inflate(R.layout.rawlistview2, null);
LL = (LinearLayout) ReturnView.findViewById(R.id.LLOne);
txtOne = (TextView) ReturnView
.findViewById(R.id.textView1list2);
txtTwo = (TextView) ReturnView.findViewById(R.id.textViewlist2);
btnone = (Button) ReturnView.findViewById(R.id.button1);
btnTwo = (Button) ReturnView.findViewById(R.id.button2);
btnThree = (Button) ReturnView.findViewById(R.id.button3);
btnone.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ButtonOne", btnone.getText().toString());
}
});
btnTwo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ButtonTwo", btnTwo.getText().toString());
}
});
btnThree.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.i("ButtonThree", btnThree.getText().toString());
}
});
}
return ReturnView;
}
}
}
Upvotes: 1